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

apache / iotdb / #9968

31 Aug 2023 02:59AM UTC coverage: 47.703% (+0.003%) from 47.7%
#9968

push

travis_ci

web-flow
[To rel/1.2] Refactoring DeleteOutdatedFileTask in WalNode (#10992)

99 of 99 new or added lines in 1 file covered. (100.0%)

80192 of 168108 relevant lines covered (47.7%)

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", IoTDBConstant.IOTDB_PACKAGE, IoTDBConstant.JMX_TYPE, getID().getJmxName());
1✔
42
  protected AbstractThriftServiceThread thriftServiceThread;
43
  protected TProcessor processor;
44

45
  private CountDownLatch stopLatch;
46

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

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

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

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

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

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

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

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

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

96
  public abstract String getBindIP();
97

98
  public abstract int getBindPort();
99

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

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

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

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

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

153
  public void stopService() {
154
    if (STATUS_DOWN.equals(getRPCServiceStatus())) {
1✔
155
      logger.info("{}: {} isn't running now", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName());
×
156
      return;
×
157
    }
158
    logger.info("{}: closing {}...", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName());
1✔
159
    if (thriftServiceThread != null) {
1✔
160
      thriftServiceThread.close();
1✔
161
    }
162
    try {
163
      stopLatch.await();
1✔
164
      reset();
1✔
165
      logger.info(
1✔
166
          "{}: close {} successfully", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName());
1✔
167
    } catch (InterruptedException e) {
×
168
      logger.error(
×
169
          "{}: close {} failed because: ", IoTDBConstant.GLOBAL_DB_NAME, this.getID().getName(), e);
×
170
      Thread.currentThread().interrupt();
×
171
    }
1✔
172
  }
1✔
173
}
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