forked from ucbrise/confluo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfluoProducer.java
191 lines (164 loc) · 5.72 KB
/
ConfluoProducer.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package confluo.streaming;
import confluo.rpc.*;
import org.apache.thrift.TApplicationException;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Properties;
import java.util.Random;
/**
* not thread safe
*
* */
public class ConfluoProducer {
Logger logger= LoggerFactory.getLogger(ConfluoProducer.class);
private static final char[] encodeTable = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9'};
private Properties properties=new Properties();
private String topic;
private String host;
private int port;
private long totalProduceNum;
private long produceMaxNum;
private int produceBathSize;
private boolean batchEnable=false;
private int messageSize;
private rpc_storage_mode storageMode;
private String produceSchema;
private RpcClient client;
private MessageBatchBuilder batchBuilder;
private Schema curSchema;
private int batched;
public ConfluoProducer(){
URL propertiesUrl=ConfluoProducer.class.getClassLoader().getResource("mq.properties");
try {
InputStream in = propertiesUrl.openStream();
properties.load(in);
host=properties.getProperty("mq.server.address");
port=Integer.valueOf(properties.getProperty("mq.server.port"));
topic=properties.getProperty("mq.produce.topic");
produceMaxNum =Integer.valueOf(properties.getProperty("mq.produce.message.maxNum"));
storageMode=rpc_storage_mode.valueOf(properties.getProperty("mq.produce.archive.mode"));
messageSize=Integer.valueOf(properties.getProperty("mq.produce.message.size","64"));
produceSchema=getSchema(messageSize-8);
batchEnable=Boolean.valueOf(properties.getProperty("mq.produce.batch.enable","false"));
produceBathSize=Integer.valueOf(properties.getProperty("mq.produce.batch.size","20"));
logger.info(String.format("\n ------------------- \n producer config \n server address: %s,port:%d,topic:%s,schema:%s \n mutlilog:%s ;batch:%s; batchSize:%d;storage mode:%s \n ----------------",
host,port,topic,produceSchema,topic,batchEnable,produceBathSize,storageMode));
}catch (IOException e){
logger.error("io error",e);
}
}
/**
* create a topic or reuse existed topic
*
**/
public void start() throws TException{
client= new RpcClient(host, port);
try {
long atomicLogId = client.getAtomicMultilog(topic);
client.setCurrentAtomicMultilog(topic);
logger.info(String.format("%s exist,atomic multilog id %d,reuse it now", topic, atomicLogId));
} catch (TApplicationException e) {
client.createAtomicMultilog(topic, produceSchema, storageMode);
} catch (TException e){
logger.info("start error",e);
}
curSchema=client.getSchema();
if(batchEnable) {
batchBuilder = new MessageBatchBuilder(client.getSchema());
}
}
/**
* default a timestamp
* actual schema
* { long:a;msg:STRING(len)}
*
**/
private String getSchema(int len){
return String.format("{msg: STRING(%d)}",len);
}
/**
* @return current schema
**/
public Schema getSchema(){
return curSchema;
}
/**
* @param len message length
* @return byte buffer message
*
**/
public ByteBuffer byteMessage(int len){
Random rd = new Random();
ByteBuffer buffer=ByteBuffer.allocate(len);
buffer.order(ByteOrder.LITTLE_ENDIAN);
byte b;
// skip 8 byte for timestamp
buffer.position(8);
for (int i =8; i < len; i++) {
b=(byte) encodeTable[rd.nextInt(36)];
buffer.put(b);
}
return buffer;
}
/**
* produce a message
*
**/
public void produce(ByteBuffer message) throws TException{
try {
message.putLong(0, System.currentTimeMillis());
message.clear();
if(batchEnable) {
batchBuilder.addRecord(message);
batched++;
if(batched>=produceBathSize){
long offset=client.appendBatch(batchBuilder.getBatch());
//logger.info("write offset:"+offset);
batchBuilder.clear();
totalProduceNum+=batched;
batched=0;
}
}else {
client.appendRaw(message);
totalProduceNum++;
}
}catch (IOException e){
logger.error("io error",e);
}
}
/**
* @return get total produced messages
*
**/
public long getTotalProduceNum() {
return totalProduceNum;
}
/**
* flush the last batch message
*
**/
public void flush() throws IOException,TException{
if(batchEnable&&batched>0){
long offset=client.appendBatch(batchBuilder.getBatch());
//logger.info("write offset:"+offset);
batchBuilder.clear();
totalProduceNum+=batched;
batched=0;
}
}
/**
*
**/
public void stop() throws TException{
client.disconnect();
client.close();
}
}