BaiChuanMainV2.java 7.04 KB
Newer Older
wang-jinfeng committed
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
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.alibaba.fastjson.JSONObject;
import com.google.common.util.concurrent.*;
import mobvista.dmp.common.ClickHouseJdbc;
import mobvista.dmp.util.DateUtil;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import ru.yandex.clickhouse.ClickHouseConnection;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @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 BaiChuanMainV2 {

    private static String dt = DateUtil.format(new Date(), "yyyy-MM-dd");
wang-jinfeng committed
35 36
    static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(100, 200, 500, TimeUnit.MILLISECONDS,
            new LinkedBlockingDeque<>(200), new CustomizableThreadFactory("BaiChuan"), new ThreadPoolExecutor.CallerRunsPolicy());
wang-jinfeng committed
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

    public static void main(String[] args) throws JoranException, InterruptedException {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(context);
        context.reset();
        configurator.doConfigure(BaiChuanMainV2.class.getClassLoader().getResourceAsStream("logback-syslog.xml"));
        Logger logger = context.getLogger("baichuan");

        String appId = "2";
        String appOs = "1";
        int fors = 200;
        int concurrent = 2000;
        if (args.length >= 4) {
            appId = args[0];
            appOs = args[1];
            fors = Integer.parseInt(args[2]);
            concurrent = Integer.parseInt(args[3]);
            dt = args[4];
        }
        long start = System.currentTimeMillis();
        List<ListenableFuture<AsoDevice>> futures = new CopyOnWriteArrayList<>();

        for (int c = 1; c <= fors; c++) {
            long min_start = System.currentTimeMillis();
            ListeningExecutorService listeningExecutor = MoreExecutors.listeningDecorator(poolExecutor);
            MoreExecutors.addDelayedShutdownHook(listeningExecutor, 500, TimeUnit.SECONDS);

            ClickHouseConnection connection = null;
            ResultSet resultSet = null;
            try {
                connection = ClickHouseJdbc.connection();
            } catch (SQLException e) {
                try {
                    connection = ClickHouseJdbc.connection();
                } catch (SQLException ex) {
                    logger.info("ClickHouse Connection Failure!");
                }
            }
            String date = DateUtil.getDayByString(dt, "yyyy-MM-dd", -1);
            try {
                resultSet = ClickHouseUtils.queryAli(connection, date, appId, appOs, concurrent * c, concurrent);
            } catch (SQLException e) {
                logger.info("ClickHouse Query Failure!");
            }
            List<AsoDevice> resultList = new CopyOnWriteArrayList<>();
            try {
                while (resultSet.next()) {
                    String deviceId = resultSet.getString("device_id");
                    AsoDevice asoDevice = returnClass(appId, appOs, deviceId);
                    int finalAppOs = Integer.parseInt(appOs);
                    ListenableFuture listenableFuture = listeningExecutor.submit(() -> {
                        JSONObject jsonObject = BaiChuanServerV2.request(logger, asoDevice, finalAppOs);
                        if (jsonObject.getBoolean("result")) {
                            asoDevice.setPackageName("0");
                        }
                        return asoDevice;
                    });

                    Futures.addCallback(listenableFuture, new FutureCallback<AsoDevice>() {
                        @Override
                        public void onSuccess(AsoDevice asoDevices) {
                            resultList.add(asoDevices);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            asoDevice.setPackageName("0");
                            resultList.add(asoDevice);
                        }
                    });
                    futures.add(listenableFuture);
                }
            } catch (SQLException e) {
                logger.info("ClickHouse Execute Failure!");
            }
            try {
                ClickHouseUtils.insert2(connection, futures);
                logger.info("Times -->> " + c + ", ClickHouse Insert Success! Size -->> " + futures.size());
            } catch (SQLException e) {
                logger.info("ClickHouse Insert Failure!");
            }
            futures = new CopyOnWriteArrayList<>();
            long min_end = System.currentTimeMillis();
            logger.info("Times -->> " + c + ", Runtime -->> " + (min_end - min_start));
        }
        poolExecutor.shutdown();
        long end = System.currentTimeMillis();
        logger.info("all runtime -->> " + (end - start));
    }

    private static AsoDevice returnClass(String appId, String appOs, String deviceId) {
        AsoDevice asoDevice = new AsoDevice();
        if ("1".equals(appId)) {
            if ("1".equals(appOs)) {
                asoDevice.setDeviceId(deviceId);
                asoDevice.setDeviceType("imei");
                asoDevice.setPlatform("android");
                asoDevice.setPackageName("com.tmall.wireless");
            } else if ("2".equals(appOs)) {
                asoDevice.setDeviceId(deviceId);
                asoDevice.setDeviceType("idfa");
                asoDevice.setPlatform("ios");
                asoDevice.setPackageName("518966501");
            } else {
                asoDevice.setDeviceId(deviceId);
                asoDevice.setDeviceType("imeimd5");
                asoDevice.setPlatform("android");
                asoDevice.setPackageName("com.tmall.wireless");
            }
        } else {
            if ("1".equals(appOs)) {
                asoDevice.setDeviceId(deviceId);
                asoDevice.setDeviceType("imei");
                asoDevice.setPlatform("android");
                asoDevice.setPackageName("com.taobao.taobao");
            } else if ("2".equals(appOs)) {
                asoDevice.setDeviceId(deviceId);
                asoDevice.setDeviceType("idfa");
                asoDevice.setPlatform("ios");
                asoDevice.setPackageName("387682726");
            } else {
                asoDevice.setDeviceId(deviceId);
                asoDevice.setDeviceType("imeimd5");
                asoDevice.setPlatform("android");
                asoDevice.setPackageName("com.taobao.taobao");
            }
        }
        asoDevice.setDt(dt);
        return asoDevice;
    }
}