es

public interface ConfigContants {
String ENV_ES_DATA = "ES_DATA";
String DEFAULT_ES_DATA_PATH = "/srv/elasticsearch";

String LOG_ENV_ES_DATA = "LOG_ES_DATA";
String LOG_MGR_ES_DATA_PATH = "./";

}

public interface EsConfManager {
boolean initConfOk();
}

@Component
public class EsConfManagerImpl implements EsConfManager {

private final AtomicBoolean esConfInitOk = new AtomicBoolean(false);

private static Properties envPro;
@Override
public boolean initConfOk() {
if (!esConfInitOk.get()) {
System.out.println("es not ok");
}
return esConfInitOk.get();
}

@PostConstruct
public void start() {
esConfInitOk.set(false);
envPro = System.getProperties();
ThreadUtils.submitAndReturnFuture("es-init", () -> {
initEsEnv();
try {
initEsDataDir();
} catch (IOException e) {
e.printStackTrace();
}
esConfInitOk.set(true);
});
}

private void initEsEnv() {
// export env
if (!envPro.contains(ConfigContants.ENV_ES_DATA)) {
System.setProperty(ConfigContants.ENV_ES_DATA, ConfigContants.DEFAULT_ES_DATA_PATH);
}
if (!envPro.getProperty(ConfigContants.DEFAULT_ES_DATA_PATH).equals(ConfigContants.LOG_ENV_ES_DATA)) {
System.setProperty(ConfigContants.LOG_ENV_ES_DATA, envPro.getProperty(ConfigContants.ENV_ES_DATA));
}
}

private void initEsDataDir() throws IOException {
File esDir = new File(ConfigContants.LOG_ENV_ES_DATA + File.separator + "data");
if (!esDir.exists()) {
boolean isk = esDir.mkdir();
Files.setPosixFilePermissions(esDir.toPath(), PosixFilePermissions.fromString("550"));
}
}
}

@Component
public class EsMonitorAdatpter {
private final AtomicBoolean esSummaryConfig = new AtomicBoolean(false);
private final AtomicBoolean esStatus = new AtomicBoolean(false);

@Autowired
private LogMgrDm logMgrDm;

@Autowired
private EsProcessAdatper esProcessAdatper;

@Autowired
private EsConfManager esConfManager;

@Autowired
private LogMgrManager logMgrManager;

public synchronized boolean esSummaryIsOn() {
if (!esSummaryConfig.get()) {
System.out.println("monitor off");
}
return esSummaryConfig.get();
}

@PostConstruct
public void init() throws InterruptedException {
while (!logMgrManager.logMgrConfOk() || !esConfManager.initConfOk()) {
Thread.sleep(2000);
}
ThreadUtils.scheduleWithFixedDelay(() -> {
if ("ok".equals(logMgrDm.getConfig())) {
esSummaryConfig.set(true);
} else {
esSummaryConfig.set(false);
}
controlEs();
}, 10L, 30L, TimeUnit.SECONDS);

}


private void controlEs() {
//do sth
int result = monitorEsProcess();
if (result == 0 && !esSummaryConfig.get()) {
esProcessAdatper.stopEs();
}
if (result == 1 && esSummaryConfig.get()) {
esProcessAdatper.startEs();
}
}

private int monitorEsProcess() {
String cmd = "ps -ef|grep el";
return 2;

}



public synchronized void startEs() {
System.out.println("start es");
}

public boolean esIsRun() {
if (!esStatus.get()) {
System.out.println("not run");
}
return esStatus.get();
}
}

public class EsProcessAdatper {

public void startEs() {
//start es
}

public void stopEs() {
//stop es
}
}

public interface LogMgrManager {

boolean logMgrConfOk();

boolean isLogMgrPrepared();
}


public class LogMgrManagerImpl implements LogMgrManager {

private final AtomicBoolean logMgrConf = new AtomicBoolean(false);

private final AtomicBoolean logMgrPrepared = new AtomicBoolean(false);


@Override
public boolean logMgrConfOk() {
if (!logMgrConf.get()) {
System.out.println("not ok");
}
return logMgrConf.get();
}

@Override
public boolean isLogMgrPrepared() {
if (!logMgrPrepared.get()) {
System.out.println("no prepared");
}
return logMgrPrepared.get();
}

@PostConstruct
public void init() {
logMgrConf.set(false);
logMgrPrepared.set(false);
ThreadUtils.submitAndReturnFuture("init LogMgr", () -> {
updateDbConf();
logMgrConf.set(true);

// waiting latest conf is ok
while (!downLoadConf()) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

// latest conf is ok,update db again.
updateDbConf();
});

}

private boolean downLoadConf() {
// waiting config update from configmgr .

logMgrPrepared.set(true);
return true;
}

private void updateDbConf() {
// create db config or default
System.out.println("update db conf");
}
}
posted @ 2020-09-06 04:24  一叶风语  阅读(155)  评论(0编辑  收藏  举报