blob: d48eaaf06e621b9c7a7df1886c09ff56adbaae13 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.iotdb.confignode.conf;
import org.apache.iotdb.common.rpc.thrift.TConfigNodeLocation;
import org.apache.iotdb.common.rpc.thrift.TEndPoint;
import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.commons.conf.IoTDBConstant;
import org.apache.iotdb.commons.exception.BadNodeUrlException;
import org.apache.iotdb.confignode.client.ConfigNodeRequestType;
import org.apache.iotdb.confignode.client.sync.confignode.SyncConfigNodeClientPool;
import org.apache.iotdb.rpc.TSStatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
public class ConfigNodeRemoveCheck {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigNodeStartupCheck.class);
private static final ConfigNodeConfig CONF = ConfigNodeDescriptor.getInstance().getConf();
private final File systemPropertiesFile;
private final Properties systemProperties;
public ConfigNodeRemoveCheck() {
systemPropertiesFile =
new File(CONF.getSystemDir() + File.separator + ConfigNodeConstant.SYSTEM_FILE_NAME);
systemProperties = new Properties();
}
public TConfigNodeLocation removeCheck(TEndPoint endPoint) {
TConfigNodeLocation nodeLocation = new TConfigNodeLocation();
if (!systemPropertiesFile.exists()) {
LOGGER.error("The system properties file is not exists. IoTDB-ConfigNode is shutdown.");
return nodeLocation;
}
try (FileInputStream inputStream = new FileInputStream(systemPropertiesFile)) {
systemProperties.load(inputStream);
nodeLocation =
getConfigNodeList().stream()
.filter(e -> e.getInternalEndPoint().equals(endPoint))
.findFirst()
.orElse(null);
} catch (IOException | BadNodeUrlException e) {
LOGGER.error("Load system properties file failed.", e);
}
return nodeLocation;
}
public void removeConfigNode(TConfigNodeLocation removedNode)
throws BadNodeUrlException, IOException {
TSStatus status = new TSStatus();
for (TConfigNodeLocation configNodeLocation : getConfigNodeList()) {
status =
(TSStatus)
SyncConfigNodeClientPool.getInstance()
.sendSyncRequestToConfigNodeWithRetry(
configNodeLocation.getInternalEndPoint(),
removedNode,
ConfigNodeRequestType.REMOVE_CONFIG_NODE);
if (status.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
break;
}
}
if (status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode()) {
LOGGER.error(status.getMessage());
throw new IOException("Remove ConfigNode failed:");
}
}
/** config_node_list of confignode-system.properties */
public List<TConfigNodeLocation> getConfigNodeList() throws BadNodeUrlException, IOException {
return SystemPropertiesUtils.loadConfigNodeList();
}
public int getConsensusPort() {
return Integer.parseInt(systemProperties.getProperty(IoTDBConstant.CONSENSUS_PORT));
}
private static class ConfigNodeConfRemoveCheckHolder {
private static final ConfigNodeRemoveCheck INSTANCE = new ConfigNodeRemoveCheck();
private ConfigNodeConfRemoveCheckHolder() {
// Empty constructor
}
}
public static ConfigNodeRemoveCheck getInstance() {
return ConfigNodeRemoveCheck.ConfigNodeConfRemoveCheckHolder.INSTANCE;
}
}