Search in sources :

Example 1 with Etcd

use of com.chinaunicom.etcd.v2.Etcd in project mesosFramework by zhizuqiu.

the class EtcdServiceTest method before.

@Before
public void before() {
    // http://192.168.84.131:2379
    Etcd etcd = EtcdClient.getInstance("http://10.124.142.127:5379");
    etcdService = new EtcdService(etcd);
    Config config = new Config();
    config.setFrameworkName("dockerFramework");
    AppDataStore.setConfig(config);
}
Also used : Etcd(com.chinaunicom.etcd.v2.Etcd) EtcdService(com.chinaunicom.rundocker.service.EtcdService) Config(com.chinaunicom.rundocker.store.Config) Before(org.junit.Before)

Example 2 with Etcd

use of com.chinaunicom.etcd.v2.Etcd in project mesosFramework by zhizuqiu.

the class EtcdClient method getEnableUrl.

public static String getEnableUrl(List<String> urls) {
    String url;
    Etcd etcd;
    GsonDecoder decoder = new GsonDecoder(GSON);
    GsonEncoder encoder = new GsonEncoder(GSON);
    if (urls == null) {
        return null;
    }
    for (String url1 : urls) {
        url = url1;
        etcd = Feign.builder().encoder(encoder).decoder(decoder).errorDecoder(new EtcdErrorDecoder()).requestInterceptor(new EtcdHeadersInterceptor()).target(Etcd.class, url);
        try {
            etcd.getEtcdVersion();
            return url;
        } catch (Exception e) {
            logger.info("connect url[" + url1 + "] error! message:" + e.getMessage());
        }
    }
    return null;
}
Also used : GsonEncoder(feign.gson.GsonEncoder) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Example 3 with Etcd

use of com.chinaunicom.etcd.v2.Etcd in project mesosFramework by zhizuqiu.

the class EtcdClient method getInstance.

public static Etcd getInstance(List<String> urls) {
    if (urls == null) {
        return null;
    }
    Etcd etcd;
    GsonDecoder decoder = new GsonDecoder(GSON);
    GsonEncoder encoder = new GsonEncoder(GSON);
    for (String url : urls) {
        etcd = Feign.builder().encoder(encoder).decoder(decoder).errorDecoder(new EtcdErrorDecoder()).requestInterceptor(new EtcdHeadersInterceptor()).target(Etcd.class, url);
        try {
            etcd.getEtcdVersion();
            return etcd;
        } catch (Exception e) {
            logger.info("connect url[" + url + "] error! message:" + e.getMessage());
        }
    }
    logger.error("get etcd instance error!");
    return null;
}
Also used : GsonEncoder(feign.gson.GsonEncoder) GsonDecoder(feign.gson.GsonDecoder) IOException(java.io.IOException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Example 4 with Etcd

use of com.chinaunicom.etcd.v2.Etcd in project mesosFramework by zhizuqiu.

the class EtcdService method setStatusAtomic.

/**
 * 设置任务状态,当状态更新时间 < etcd节点的时间
 *
 * @param logTime   任务标识
 * @param jobStatus jobStatus
 * @return 成功与否
 */
public SetResult setStatusAtomic(Long logTime, DockerStatus jobStatus) {
    String key = AppDataStore.getConfig().getFrameworkName() + JOBSTATUS_PATH + Tools.getStringByJsonpathRecursion(jobStatus.getId().replaceAll("/", "."));
    DockerStatus jobStatusTemp = null;
    boolean getSuccess = false;
    Integer modifiedIndex = null;
    /*
         * JsonSyntaxException|ClientException表示节点异常,可以直接覆盖
         * ServerException|Exception表示未知异常,需要同步程序处理
        */
    try {
        ResponseNode responseNode = etcd.getKey(key);
        if (EtcdAction.GET.equals(responseNode.getAction())) {
            jobStatusTemp = new Gson().fromJson(responseNode.getNode().getValue(), DockerStatus.class);
            modifiedIndex = responseNode.getNode().getModifiedIndex();
            getSuccess = true;
        }
    } catch (JsonSyntaxException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->fromJson->JsonSyntaxException->" + e.getMessage());
        getSuccess = true;
    } catch (ClientException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->getKey->ClientException->" + e.getDetail().getMessage());
        getSuccess = true;
    } catch (ServerException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->getKey->ServerException->" + e.getDetail().getMessage());
        return HAS_EXCEPTION;
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->getKey->Exception->" + e.getMessage());
        return HAS_EXCEPTION;
    }
    if (!getSuccess) {
        return HAS_EXCEPTION;
    }
    // 如果状态更新时间<etcd节点的时间,不更新
    if (jobStatusTemp != null) {
        Double timestampTemp = jobStatusTemp.getTimestamp();
        if (timestampTemp != null) {
            Double timestamp = jobStatus.getTimestamp();
            if (timestamp < timestampTemp) {
                return UNNECESSARY;
            }
        }
    }
    boolean setSuccess = false;
    try {
        ResponseNodeAndPrevnode responseNodeAndPrevnode;
        if (modifiedIndex != null) {
            responseNodeAndPrevnode = etcd.setKeyAtomic(key, new Gson().toJson(jobStatus), String.valueOf(modifiedIndex));
        } else {
            responseNodeAndPrevnode = etcd.setKey(key, new Gson().toJson(jobStatus));
        }
        if (EtcdAction.COMPARE_AND_SWAP.equals(responseNodeAndPrevnode.getAction())) {
            setSuccess = true;
        }
    } catch (ClientException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->setKeyAtomic->ClientException->" + e.getDetail().getMessage());
        return HAS_EXCEPTION;
    } catch (ServerException e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->setKeyAtomic->ServerException->" + e.getDetail().getMessage());
        return HAS_EXCEPTION;
    } catch (Exception e) {
        logger.error("[" + logTime + "]" + "setNodeAtomic->setKeyAtomic->Exception->" + e.getMessage());
        return HAS_EXCEPTION;
    }
    return setSuccess ? SUCCESS : HAS_EXCEPTION;
}
Also used : ResponseNodeAndPrevnode(com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnode) ResponseNode(com.chinaunicom.etcd.v2.model.ResponseNode) JsonSyntaxException(com.google.gson.JsonSyntaxException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException) DockerStatus(com.chinaunicom.rundocker.bean.DockerStatus) Gson(com.google.gson.Gson) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) JsonSyntaxException(com.google.gson.JsonSyntaxException) ClientException(com.chinaunicom.etcd.v2.exception.ClientException) ServerException(com.chinaunicom.etcd.v2.exception.ServerException)

Example 5 with Etcd

use of com.chinaunicom.etcd.v2.Etcd in project mesosFramework by zhizuqiu.

the class FrameworkThread method call.

@Override
public String call() throws Exception {
    Etcd etcd = EtcdClient.getInstance(AppDataStore.getConfig().getEtcdEndpoints());
    try {
        watchClient = WatchClientBuilder.builder().etcd(etcd).rootDir(AppDataStore.getConfig().getFrameworkName()).build();
    } catch (Exception e) {
        logger.error(e.getMessage());
        watchClient.close();
        return "init watchClient error";
    }
    // 将会在这里阻塞,直到获取leader
    String leaderId = watchClient.selectLeader();
    System.out.println("onGetLeader leaderId:" + leaderId);
    /*
         维持leader状态,直到futureTask.isDone()==true,表示失去leader地位
         keeper leader发生异常时,停止两个线程
          */
    FutureTask<String> futureTask = watchClient.keepLeader((id) -> {
        logger.error("leader is lost:" + id);
        AppDataStore.shutdown();
    });
    AppDataStore.setWatchClient(watchClient);
    String frameworkId = watchClient.getFrameworkId();
    Protos.FrameworkInfo.Builder frameworkInfoBuilder = Protos.FrameworkInfo.newBuilder().setUser("").setName(AppDataStore.getConfig().getFrameworkName()).setFailoverTimeout(AppDataStore.getConfig().getFailoverTimeout()).setCheckpoint(true);
    if (AppDataStore.getConfig().getUser() != null) {
        frameworkInfoBuilder.setUser(AppDataStore.getConfig().getUser());
    }
    if (AppDataStore.getConfig().getRole() != null) {
        frameworkInfoBuilder.setRole(AppDataStore.getConfig().getRole());
        logger.info("set role:" + AppDataStore.getConfig().getRole());
        frameworkInfoBuilder.setPrincipal(AppDataStore.getConfig().getRole());
    }
    if (frameworkId != null) {
        logger.info("frameworkId:" + frameworkId);
        frameworkInfoBuilder.setId(Protos.FrameworkID.newBuilder().setValue(frameworkId));
    }
    if (AppDataStore.getInfo().getHost() != null && AppDataStore.getInfo().getPort() != null) {
        frameworkInfoBuilder.setHostname(AppDataStore.getInfo().getHost());
        frameworkInfoBuilder.setWebuiUrl("http://" + AppDataStore.getInfo().getHost() + ":" + AppDataStore.getInfo().getPort() + "/");
    }
    Scheduler dockerScheduler = new DockerScheduler(etcd);
    SchedulerDriver driver = new MesosSchedulerDriver(dockerScheduler, frameworkInfoBuilder.build(), AppDataStore.getConfig().getMesosZkUrl());
    // 这里如果找不到meososlib会阻塞在这里,下个方法不走
    int status = driver.run() == Protos.Status.DRIVER_STOPPED ? 0 : 1;
    logger.info(status);
    return "framework thread exit";
}
Also used : Etcd(com.chinaunicom.etcd.v2.Etcd) Scheduler(org.apache.mesos.Scheduler) MesosSchedulerDriver(org.apache.mesos.MesosSchedulerDriver) SchedulerDriver(org.apache.mesos.SchedulerDriver) MesosSchedulerDriver(org.apache.mesos.MesosSchedulerDriver)

Aggregations

ClientException (com.chinaunicom.etcd.v2.exception.ClientException)3 ServerException (com.chinaunicom.etcd.v2.exception.ServerException)3 Etcd (com.chinaunicom.etcd.v2.Etcd)2 GsonDecoder (feign.gson.GsonDecoder)2 GsonEncoder (feign.gson.GsonEncoder)2 IOException (java.io.IOException)2 ResponseNode (com.chinaunicom.etcd.v2.model.ResponseNode)1 ResponseNodeAndPrevnode (com.chinaunicom.etcd.v2.model.ResponseNodeAndPrevnode)1 DockerStatus (com.chinaunicom.rundocker.bean.DockerStatus)1 EtcdService (com.chinaunicom.rundocker.service.EtcdService)1 Config (com.chinaunicom.rundocker.store.Config)1 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 MesosSchedulerDriver (org.apache.mesos.MesosSchedulerDriver)1 Scheduler (org.apache.mesos.Scheduler)1 SchedulerDriver (org.apache.mesos.SchedulerDriver)1 Before (org.junit.Before)1