Search in sources :

Example 1 with RealtimeLoggerCollectorAppender

use of com.qlangtech.tis.log.RealtimeLoggerCollectorAppender in project tis by qlangtech.

the class TestIncrStatusServer method testServer.

public void testServer() throws Exception {
    int taskid = 123;
    String appname = "baisuitest";
    AtomicBoolean clientClosed = new AtomicBoolean(false);
    RegisterMonitorEventHook eventHook = new RegisterMonitorEventHook() {

        boolean hasStartSession = false;

        boolean hasCloseSession = false;

        int send2ClientCount = 0;

        int readFromFileTailerCount = 0;

        @Override
        public void send2ClientFromFileTailer(File logFile, PExecuteState s) {
            readFromFileTailerCount++;
        }

        @Override
        void startSession() {
            hasStartSession = true;
        }

        @Override
        void send2Client(PExecuteState s, LoggingEvent e) {
            send2ClientCount++;
        }

        @Override
        void closeSession() {
            hasCloseSession = true;
        }

        public void validateExpect() {
            assertTrue(this.hasStartSession);
            assertTrue(this.hasCloseSession);
            assertTrue("send2ClientCount shall big than 0", send2ClientCount > 0);
            assertTrue("read from file buffer", readFromFileTailerCount > 0);
        }
    };
    FullBuildStatCollectorServer.registerMonitorEventHook = eventHook;
    // 启动rpc服务端
    Runnable writeLog = () -> {
        MDC.put(IParamContext.KEY_TASK_ID, String.valueOf(taskid));
        MDC.put("app", appname);
        String logMsg = "test_log_msg";
        Logger log = LoggerFactory.getLogger(TestIncrStatusServer.class);
        while (true) {
            // 需要会向RealtimeLoggerCollectorAppender 该appender中写入
            log.info(logMsg);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
    };
    Thread tt = new Thread(writeLog);
    tt.setDaemon(true);
    tt.start();
    StatusRpcClient.AssembleSvcCompsite svc = getRpcClient();
    try {
        CountDownLatch countdown = new CountDownLatch(1);
        int[] receiveCount = new int[1];
        StreamObserver<PMonotorTarget> observer = svc.registerMonitorEvent(new ILogListener() {

            @Override
            public void sendMsg2Client(Object biz) throws IOException {
            }

            @Override
            public void read(Object event) {
                PExecuteState stat = (PExecuteState) event;
                System.out.println(stat.getTaskId() + " " + stat.getMsg());
                if (receiveCount[0]++ > 5) {
                    try {
                        // 收到服务端 5次消息之后,模拟webSocket session 关闭
                        clientClosed.set(true);
                    // throw new RuntimeException("dddddddddddddd");
                    } finally {
                        countdown.countDown();
                    }
                }
            }

            @Override
            public boolean isClosed() {
                return clientClosed.get();
            }
        });
        PMonotorTarget.Builder mt = PMonotorTarget.newBuilder();
        mt.setCollection("dummy");
        mt.setTaskid(taskid);
        mt.setLogtype(LogCollectorClient.convert(LogType.FULL.typeKind));
        PMonotorTarget mtarget = mt.build();
        observer.onNext(mtarget);
        String targetToken = FullBuildStatCollectorServer.addListener(LogCollectorClient.convert(mtarget), new RealtimeLoggerCollectorAppender.LoggerCollectorAppenderListener() {

            @Override
            public void process(RealtimeLoggerCollectorAppender.LoggingEventMeta mtarget, LoggingEvent e) {
                System.out.println("server side kk:" + e.getMessage());
            }

            @Override
            public void readLogTailer(RealtimeLoggerCollectorAppender.LoggingEventMeta meta, File logFile) {
            }

            @Override
            public boolean isClosed() {
                return clientClosed.get();
            }
        });
        assertTrue("countdown shall be execute", countdown.await(20, TimeUnit.SECONDS));
        // 需要再写几个日志
        Thread.sleep(2000l);
        RealtimeLoggerCollectorAppender.LogTypeListeners logListeners = RealtimeLoggerCollectorAppender.appenderListener.getLogTypeListeners(targetToken);
        assertNotNull("logListeners can not be null", logListeners);
        assertEquals(1, logListeners.getListenerSize());
        eventHook.validateExpect();
        System.out.println("rpc test over");
    } finally {
        svc.close();
    }
}
Also used : ILogListener(com.qlangtech.tis.trigger.jst.ILogListener) RealtimeLoggerCollectorAppender(com.qlangtech.tis.log.RealtimeLoggerCollectorAppender) Logger(org.slf4j.Logger) LoggingEvent(ch.qos.logback.classic.spi.LoggingEvent) PExecuteState(com.qlangtech.tis.rpc.grpc.log.stream.PExecuteState) StatusRpcClient(com.tis.hadoop.rpc.StatusRpcClient) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) PMonotorTarget(com.qlangtech.tis.rpc.grpc.log.stream.PMonotorTarget)

Aggregations

LoggingEvent (ch.qos.logback.classic.spi.LoggingEvent)1 RealtimeLoggerCollectorAppender (com.qlangtech.tis.log.RealtimeLoggerCollectorAppender)1 PExecuteState (com.qlangtech.tis.rpc.grpc.log.stream.PExecuteState)1 PMonotorTarget (com.qlangtech.tis.rpc.grpc.log.stream.PMonotorTarget)1 ILogListener (com.qlangtech.tis.trigger.jst.ILogListener)1 StatusRpcClient (com.tis.hadoop.rpc.StatusRpcClient)1 File (java.io.File)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Logger (org.slf4j.Logger)1