-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiProducerTableDataProvider.java
More file actions
142 lines (129 loc) · 5.15 KB
/
Copy pathMultiProducerTableDataProvider.java
File metadata and controls
142 lines (129 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright 2023 Greptime Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.greptime.bench;
import com.codahale.metrics.Timer;
import io.greptime.common.SPI;
import io.greptime.common.util.ExecutorServiceHelper;
import io.greptime.common.util.MetricsUtil;
import io.greptime.common.util.NamedThreadFactory;
import io.greptime.common.util.SystemPropertyUtil;
import io.greptime.common.util.ThreadPoolUtil;
import java.util.Iterator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicLong;
@SPI(
name = "multi_producer_table_data_provider",
priority = 1 /* newer implementation can use higher priority to override the old one */)
public class MultiProducerTableDataProvider extends RandomTableDataProvider {
private final int producerCount;
private final ExecutorService executorService;
private final BlockingQueue<Object[]> buffer = new ArrayBlockingQueue<>(100000);
{
this.producerCount = SystemPropertyUtil.getInt("multi_producer_table_data_provider.producer_count", 10);
// Total number of rows to generate, configurable via system property
this.executorService = ThreadPoolUtil.newBuilder()
.poolName("multi-producer-table-data-provider")
.enableMetric(true)
.coreThreads(this.producerCount)
.maximumThreads(this.producerCount)
.keepAliveSeconds(60L)
.workQueue(new ArrayBlockingQueue<>(512))
.threadFactory(new NamedThreadFactory("multi-producer-table-data-provider"))
.rejectedHandler(new ThreadPoolExecutor.CallerRunsPolicy())
.build();
}
@Override
public void init() {
AtomicLong rowIndex = new AtomicLong(0);
for (int i = 0; i < producerCount; i++) {
this.executorService.execute(() -> {
while (rowIndex.getAndIncrement() < rowCount()) {
Object[] row = nextRow();
try {
buffer.put(row);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
}
}
@Override
public Iterator<Object[]> rows() {
return new Iterator<Object[]>() {
private long index = 0;
@Override
public boolean hasNext() {
return index < rowCount();
}
@Override
public Object[] next() {
try {
Object[] row = buffer.take();
index++;
return row;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
};
}
@Override
public void close() throws Exception {
ExecutorServiceHelper.shutdownAndAwaitTermination(this.executorService);
}
public Object[] nextRow() {
ThreadLocalRandom random = ThreadLocalRandom.current();
Timer.Context timerContext =
MetricsUtil.timer("multi_producer_table_data_provider.next_row").time();
long logTs = System.currentTimeMillis();
String businessName = "business_" + random.nextInt(10);
String appName = "app_" + random.nextInt(100);
String hostName = "host_" + random.nextInt(1000);
String logMessage = LogTextHelper.generate2kText(random, logTs);
String logLevel = LogTextHelper.randomLogLevel(random);
String logName = "log_name_" + random.nextInt(100);
String uri = "http://example.com/path_" + random.nextInt(1000);
String traceId = "trace_" + random.nextLong(1000000);
String spanId = "span_" + random.nextLong(1000000);
String errno = "errno_" + random.nextInt(1000);
String traceFlags = "trace_flags_" + random.nextInt(1000);
String traceState = "trace_state_" + random.nextInt(1000);
String podName = "pod_" + random.nextInt(1000);
timerContext.stop();
MetricsUtil.histogram("random_table_data_provider.log_message_length").update(logMessage.length());
return new Object[] {
logTs,
businessName,
appName,
hostName,
logMessage,
logLevel,
logName,
uri,
traceId,
spanId,
errno,
traceFlags,
traceState,
podName
};
}
}