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
192
193
194
195
196
197
198
package mobvista.dmp.datasource.baichuan;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.taobao.api.ApiException;
import com.taobao.api.response.AlibabaBaichuanAsoQueryResponse;
import mobvista.dmp.common.ClickHouseJdbc;
import mobvista.dmp.util.DateUtil;
import org.slf4j.LoggerFactory;
import ru.yandex.clickhouse.ClickHouseConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
/**
* @package: mobvista.dmp.datasource.baichuan
* @author: wangjf
* @date: 2019-08-31
* @time: 07:49
* @email: jinfeng.wang@mobvista.com
* @phone: 152-1062-7698
*/
public class BaiChuanMain {
private static Set<AsoDevice> deviceInfoSet = new CopyOnWriteArraySet<>();
private static String dt = DateUtil.format(new Date(), "yyyy-MM-dd");
public static void main(String[] args) throws JoranException {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
configurator.doConfigure(BaiChuanServer.class.getClassLoader().getResourceAsStream("logback-syslog.xml"));
Logger logger = context.getLogger("baichuan");
String appId = "2";
String appOs = "2";
int fors = 1500;
if (args.length >= 3) {
appId = args[0];
appOs = args[1];
fors = Integer.parseInt(args[2]);
dt = args[3];
}
long start = System.currentTimeMillis();
for (int c = 0; c < fors; c++) {
Map<Integer, Set<String>> map = Collections.synchronizedMap(new HashMap<>(10));
Random random = new Random();
int finalIpId = random.nextInt(3);
String finalAppId = appId;
String finalAppOs = appOs;
// ips[new Random().nextInt(2)]
String[] ips = ClickHouseJdbc.SET_VALUES[finalIpId].split(":");
// ClickHouseDataSource dataSource = new ClickHouseDataSource(ClickHouseJdbc.URL.replace("host", ips[new Random().nextInt(2)]), properties);
// ClickHouseConnection connection = null;
// ResultSet resultSet;
ClickHouseConnection connection = null;
ResultSet resultSet;
try {
connection = ClickHouseJdbc.connection();
} catch (SQLException | InterruptedException e) {
try {
connection = ClickHouseJdbc.connection();
} catch (SQLException | InterruptedException ex) {
logger.info("ClickHouse Connection Failure!");
}
}
try {
String date = DateUtil.getDayByString(dt, "yyyy-MM-dd", -1);
assert connection != null;
resultSet = ClickHouseUtils.query(connection, date, finalAppId, finalAppOs, 4000 * c, 4000);
int i = 0;
logger.info(String.valueOf(start));
Set<String> set = Collections.synchronizedSet(new HashSet<>());
while (resultSet.next()) {
set.add(resultSet.getString("device_id"));
/**
* the array size of deviceInfoList must be less than 20.so,size of deviceId must less than 20
*/
if (set.size() % 20 == 0) {
map.put(i, set);
set = Collections.synchronizedSet(new HashSet<>());
i = i + 1;
}
}
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("ali-%d").build();
/**
* 因百川并发限制,设置总线程数为 200
*/
ExecutorService aliPool = new ThreadPoolExecutor(200, 300,
120L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
for (int reqId = 0; reqId < 200; reqId++) {
int finalReqId = reqId;
aliPool.execute(() -> {
// runJob(finalIpId, map.get(finalReqId));
try {
if (map.keySet().contains(finalReqId)) {
runRequest(finalAppId, finalAppOs, map.get(finalReqId));
}
Thread.sleep(200);
} catch (ApiException | InterruptedException e) {
e.printStackTrace();
}
});
}
ClickHouseUtils.insert(connection, new AtomicReference<>(deviceInfoSet));
logger.info("forId = " + c + ",insert ipId = " + finalIpId + ",deviceInfoSet.size = " + deviceInfoSet.size());
deviceInfoSet = new CopyOnWriteArraySet<>();
aliPool.shutdown();
Thread.sleep(200);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
Thread.sleep(200);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
}
}
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
logger.info("all runtime ==>> " + (end - start));
}
private static void runJob(int ipId, Set<String> deviceSet) {
Random random = new Random();
int ran = random.nextInt(2);
for (String deviceId : deviceSet) {
AsoDevice asoDevice = new AsoDevice();
asoDevice.setDeviceType("idfa");
asoDevice.setPlatform("ios");
asoDevice.setPackageName("387682726");
asoDevice.setDeviceId(deviceId);
asoDevice.setDt(DateUtil.format(new Date(), "yyyy-MM-dd"));
if (ran == 0) {
deviceInfoSet.add(asoDevice);
}
}
}
private static void runRequest(String appId, String appOs, Set<String> deviceSet) throws ApiException {
AlibabaBaichuanAsoQueryResponse rsp = BaiChuanServer.request(appId, appOs, deviceSet);
if (rsp != null && rsp.getResult() != null && rsp.getResult().getSuccess()) {
for (AlibabaBaichuanAsoQueryResponse.AsoDeviceCheckResult checkResult : rsp.getResult().getResults()) {
AsoDevice asoDevice = new AsoDevice();
if ("1".equals(appId)) {
if ("1".equals(appOs)) {
asoDevice.setDeviceId(checkResult.getImei());
asoDevice.setDeviceType("imei");
asoDevice.setPlatform("android");
asoDevice.setPackageName("com.tmall.wireless");
} else {
asoDevice.setDeviceId(checkResult.getIdfa());
asoDevice.setDeviceType("idfa");
asoDevice.setPlatform("ios");
asoDevice.setPackageName("518966501");
}
} else {
if ("1".equals(appOs)) {
asoDevice.setDeviceId(checkResult.getImei());
asoDevice.setDeviceType("imei");
asoDevice.setPlatform("android");
asoDevice.setPackageName("com.taobao.taobao");
} else {
asoDevice.setDeviceId(checkResult.getIdfa());
asoDevice.setDeviceType("idfa");
asoDevice.setPlatform("ios");
asoDevice.setPackageName("387682726");
}
}
// asoDevice.setDt(DateUtil.format(new Date(), "yyyy-MM-dd"));
asoDevice.setDt(dt);
if (checkResult.getIsNewDevice()) {
asoDevice.setPackageName("0");
}
deviceInfoSet.add(asoDevice);
}
}
}
}