• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

apache / iotdb / #10042

09 Sep 2023 11:52AM UTC coverage: 47.672% (-0.04%) from 47.711%
#10042

push

travis_ci

web-flow
[auth].fix cluster auth IT. (#11107)

80746 of 169380 relevant lines covered (47.67%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.39
/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/ThriftService.java
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements.  See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership.  The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License.  You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19

20
package org.apache.iotdb.commons.service;
21

22
import org.apache.iotdb.commons.conf.IoTDBConstant;
23
import org.apache.iotdb.commons.exception.StartupException;
24

25
import org.apache.thrift.TProcessor;
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28

29
import java.lang.reflect.InvocationTargetException;
30
import java.util.concurrent.CountDownLatch;
31

32
public abstract class ThriftService implements IService {
1✔
33

34
  private static final Logger logger = LoggerFactory.getLogger(ThriftService.class);
1✔
35

36
  public static final String STATUS_UP = "UP";
37
  public static final String STATUS_DOWN = "DOWN";
38

39
  protected String mbeanName =
1✔
40
      String.format(
1✔
41
          "%s:%s=%s",
42
          IoTDBConstant.IOTDB_SERVICE_JMX_NAME, IoTDBConstant.JMX_TYPE, getID().getJmxName());
1✔
43
  protected AbstractThriftServiceThread thriftServiceThread;
44
  protected TProcessor processor;
45

46
  private CountDownLatch stopLatch;
47

48
  public String getRPCServiceStatus() {
49
    if (thriftServiceThread == null) {
1✔
50
      logger.debug("Start latch is null when getting status");
1✔
51
    } else {
52
      logger.debug("Start status is {} when getting status", thriftServiceThread.isServing());
1✔
53
    }
54
    if (stopLatch == null) {
1✔
55
      logger.debug("Stop latch is null when getting status");
1✔
56
    } else {
57
      logger.debug("Stop latch is {} when getting status", stopLatch.getCount());
1✔
58
    }
59

60
    if (thriftServiceThread != null && thriftServiceThread.isServing()) {
1✔
61
      return STATUS_UP;
1✔
62
    } else {
63
      return STATUS_DOWN;
1✔
64
    }
65
  }
66

67
  @Override
68
  public void start() throws StartupException {
69
    JMXService.registerMBean(this, mbeanName);
1✔
70
    startService();
1✔
71
  }
1✔
72

73
  @Override
74
  public void stop() {
75
    stopService();
1✔
76
    JMXService.deregisterMBean(mbeanName);
1✔
77
  }
1✔
78

79
  boolean setSyncedImpl = false;
1✔
80
  boolean setAsyncedImpl = false;
1✔
81

82
  public void initSyncedServiceImpl(Object serviceImpl) {
83
    setSyncedImpl = true;
1✔
84
  }
1✔
85

86
  public void initAsyncedServiceImpl(Object serviceImpl) {
87
    setAsyncedImpl = true;
1✔
88
  }
1✔
89

90
  public abstract void initTProcessor()
91
      throws ClassNotFoundException, IllegalAccessException, InstantiationException,
92
          NoSuchMethodException, InvocationTargetException;
93

94
  public abstract void initThriftServiceThread()
95
      throws IllegalAccessException, InstantiationException, ClassNotFoundException;
96

97
  public abstract String getBindIP();
98

99
  public abstract int getBindPort();
100

101
  @SuppressWarnings("squid:S2276")
102
  public void startService() throws StartupException {
103
    if (STATUS_UP.equals(getRPCServiceStatus())) {
1✔
104
      logger.info(
×
105
          "{}: {} has been already running now",
106
          IoTDBConstant.GLOBAL_DB_NAME,
107
          this.getID().getName());
×
108
      return;
×
109
    }
110
    logger.info("{}: start {}...", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName());
1✔
111
    try {
112
      reset();
1✔
113
      initTProcessor();
1✔
114
      if (!setSyncedImpl && !setAsyncedImpl) {
1✔
115
        throw new StartupException(
×
116
            getID().getName(), "At least one service implementataion should be set.");
×
117
      }
118
      initThriftServiceThread();
1✔
119
      thriftServiceThread.setThreadStopLatch(stopLatch);
1✔
120
      thriftServiceThread.start();
1✔
121

122
      while (!thriftServiceThread.isServing()) {
1✔
123
        // sleep 100ms for waiting the rpc server start.
124
        Thread.sleep(100);
1✔
125
      }
126
    } catch (InterruptedException
×
127
        | ClassNotFoundException
128
        | IllegalAccessException
129
        | InstantiationException
130
        | NoSuchMethodException
131
        | InvocationTargetException e) {
132
      Thread.currentThread().interrupt();
×
133
      throw new StartupException(this.getID().getName(), e.getMessage());
×
134
    }
1✔
135

136
    logger.info(
1✔
137
        "{}: start {} successfully, listening on ip {} port {}",
138
        IoTDBConstant.GLOBAL_DB_NAME,
139
        this.getID().getName(),
1✔
140
        getBindIP(),
1✔
141
        getBindPort());
1✔
142
  }
1✔
143

144
  private void reset() {
145
    thriftServiceThread = null;
1✔
146
    stopLatch = new CountDownLatch(1);
1✔
147
  }
1✔
148

149
  public void restartService() throws StartupException {
150
    stopService();
×
151
    startService();
×
152
  }
×
153

154
  public void stopService() {
155
    if (STATUS_DOWN.equals(getRPCServiceStatus())) {
1✔
156
      logger.info("{}: {} isn't running now", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName());
×
157
      return;
×
158
    }
159
    logger.info("{}: closing {}...", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName());
1✔
160
    if (thriftServiceThread != null) {
1✔
161
      thriftServiceThread.close();
1✔
162
    }
163
    try {
164
      stopLatch.await();
1✔
165
      reset();
1✔
166
      logger.info(
1✔
167
          "{}: close {} successfully", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName());
1✔
168
    } catch (InterruptedException e) {
×
169
      logger.error(
×
170
          "{}: close {} failed because: ", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName(), e);
×
171
      Thread.currentThread().interrupt();
×
172
    }
1✔
173
  }
1✔
174
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc