@Override
  public <S extends StreamDefinition> S save(S entity) {
    try {
      Map<String, String> map = new HashMap<>();
      map.put(DEFINITION_KEY, entity.getDefinition());

      map.put(
          MODULE_DEFINITIONS_KEY, objectWriter.writeValueAsString(entity.getModuleDefinitions()));

      CuratorFramework client = zkConnection.getClient();
      String path = Paths.build(Paths.STREAMS, entity.getName());
      byte[] binary = ZooKeeperUtils.mapToBytes(map);

      BackgroundPathAndBytesable<?> op =
          client.checkExists().forPath(path) == null ? client.create() : client.setData();

      op.forPath(path, binary);

      logger.trace("Saved stream {} with properties {}", path, map);

      StreamDefinitionRepositoryUtils.saveDependencies(moduleDependencyRepository, entity);
    } catch (Exception e) {
      // NodeExistsException indicates that we tried to create the
      // path just after another thread/jvm successfully created it
      ZooKeeperUtils.wrapAndThrowIgnoring(e, NodeExistsException.class);
    }
    return entity;
  }
 /**
  * Set data that node should set in ZK also writes the data to the node
  *
  * @param data new data value
  * @throws Exception errors
  */
 public void setData(byte[] data) throws Exception {
   data = Preconditions.checkNotNull(data, "data cannot be null");
   this.data.set(Arrays.copyOf(data, data.length));
   if (isActive()) {
     client.setData().inBackground().forPath(getActualPath(), getData());
   }
 }
  private void processBackgroundCallback(CuratorEvent event) throws Exception {
    String path = null;
    boolean nodeExists = false;
    if (event.getResultCode() == KeeperException.Code.NODEEXISTS.intValue()) {
      path = event.getPath();
      nodeExists = true;
    } else if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
      path = event.getName();
    } else if (event.getResultCode() == KeeperException.Code.NOAUTH.intValue()) {
      log.warn("Client does not have authorisation to write node at path {}", event.getPath());
      authFailure.set(true);
      return;
    }
    if (path != null) {
      authFailure.set(false);
      nodePath.set(path);
      watchNode();

      if (nodeExists) {
        client.setData().inBackground(setDataCallback).forPath(getActualPath(), getData());
      } else {
        initialisationComplete();
      }
    } else {
      createNode();
    }
  }
 public static void setOffsetInZooKeeper(
     CuratorFramework curatorClient, String groupId, String topic, int partition, long offset)
     throws Exception {
   ZKGroupTopicDirs topicDirs = new ZKGroupTopicDirs(groupId, topic);
   String path = topicDirs.consumerOffsetDir() + "/" + partition;
   curatorClient.newNamespaceAwareEnsurePath(path).ensure(curatorClient.getZookeeperClient());
   byte[] data = Long.toString(offset).getBytes();
   curatorClient.setData().forPath(path, data);
 }
 public void updateLastFinishTime(int partitionId, Long lastFinishTime) {
   String path = this.zkRoot + "/" + partitionId + "/" + ZNODE_LAST_FINISH_TIME;
   try {
     if (curator.checkExists().forPath(path) == null) {
       curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path);
     }
     curator.setData().forPath(path, lastFinishTime.toString().getBytes("UTF-8"));
   } catch (Exception e) {
     LOG.error("failed to update last finish time {}", e);
   }
 }
 public static void setDataAsync(CuratorFramework client, String path, byte[] payload)
     throws Exception {
   CuratorListener listener =
       new CuratorListener() {
         @Override
         public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
           // examine event for details
         }
       };
   client.getCuratorListenable().addListener(listener);
   client.setData().inBackground().forPath(path, payload);
 }
 /**
  * 更新roomprocessornode临时子节点数据,同时更新backup中数据
  *
  * @param nodename
  * @param value
  * @return
  */
 private boolean updateRoomProcessorChildNode(String nodename, String value) {
   logger.debug(String.format("更新roomprocessornode子节点%s数据,值:%s", nodename, value));
   try {
     String path = ROOMPROCESSORNODE + "/" + nodename;
     backup.put(path, value);
     client.setData().forPath(path, value.getBytes(charset));
     return true;
   } catch (Exception e) {
     logger.error("ZooKeeperWrapper.updateRed5ChildNode出现异常", e);
     return false;
   }
 }
 /**
  * 更新房间的房间信息永久节点数据
  *
  * @param roomname
  * @param value
  * @return
  */
 public boolean updateRoom(String roomname, String value) {
   try {
     logger.debug(String.format("更新房间%s数据", roomname));
     client
         .setData()
         .forPath(ROOMNODE + "/" + roomname + ROOMNODECHILDROOM, value.getBytes(charset));
     return true;
   } catch (Exception e) {
     logger.error(String.format("更新房间%s数据失败", roomname));
     return false;
   }
 }
 /**
  * 更新房间的roomprocessor临时子节点数据,并更新backup
  *
  * @param roomname
  * @param value
  * @return
  */
 public boolean updateRoomChildRPNode(String roomname, String value) {
   try {
     logger.debug(String.format("更新房间%s的子节点roomprocessor数据,值:%s", roomname, value));
     String path = ROOMNODE + "/" + roomname + ROOMPROCESSOR;
     backup.put(path, value);
     client.setData().forPath(path, value.getBytes(charset));
     return true;
   } catch (Exception e) {
     logger.error(String.format("更新房间%s的子节点roomprocessor数据失败", roomname), e);
     return false;
   }
 }
 @Override
 public void updateService(final ServiceInstance<T> service) throws Exception {
   Entry<T> entry = services.get(service.getId());
   if (entry == null) {
     throw new Exception("Service not registered: " + service);
   }
   synchronized (entry) {
     entry.service = service;
     byte[] bytes = serializer.serialize(service);
     String path = pathForInstance(service.getName(), service.getId());
     client.setData().forPath(path, bytes);
   }
 }
Exemple #11
0
 public void writeBytes(String path, byte[] bytes) {
   try {
     if (_curator.checkExists().forPath(path) == null) {
       _curator
           .create()
           .creatingParentsIfNeeded()
           .withMode(CreateMode.PERSISTENT)
           .forPath(path, bytes);
     } else {
       _curator.setData().forPath(path, bytes);
     }
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
  public boolean update(String yarnAppId, String jobId, Map<String, String> tags, AppInfo app) {
    String path = this.zkRoot + "/" + yarnAppId + "/" + jobId;
    Map<String, String> appInfo = new HashMap<>();
    appInfo.put("id", app.getId());
    appInfo.put("user", app.getUser());
    appInfo.put("name", app.getName());
    appInfo.put("queue", app.getQueue());
    appInfo.put("state", app.getState());
    appInfo.put("finalStatus", app.getFinalStatus());
    appInfo.put("progress", app.getProgress() + "");
    appInfo.put("trackingUI", app.getTrackingUI());
    appInfo.put("diagnostics", app.getDiagnostics());
    appInfo.put("trackingUrl", app.getTrackingUrl());
    appInfo.put("clusterId", app.getClusterId());
    appInfo.put("applicationType", app.getApplicationType());
    appInfo.put("startedTime", app.getStartedTime() + "");
    appInfo.put("finishedTime", app.getFinishedTime() + "");
    appInfo.put("elapsedTime", app.getElapsedTime() + "");
    appInfo.put(
        "amContainerLogs", app.getAmContainerLogs() == null ? "" : app.getAmContainerLogs());
    appInfo.put(
        "amHostHttpAddress", app.getAmHostHttpAddress() == null ? "" : app.getAmHostHttpAddress());
    appInfo.put("allocatedMB", app.getAllocatedMB() + "");
    appInfo.put("allocatedVCores", app.getAllocatedVCores() + "");
    appInfo.put("runningContainers", app.getRunningContainers() + "");

    Map<String, String> fields = new HashMap<>();
    fields.put(ENTITY_TAGS_KEY, (new JSONObject(tags)).toString());
    fields.put(APP_INFO_KEY, (new JSONObject(appInfo)).toString());
    try {
      lock.acquire();
      JSONObject object = new JSONObject(fields);
      if (curator.checkExists().forPath(path) == null) {
        curator.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path);
      }
      curator.setData().forPath(path, object.toString().getBytes("UTF-8"));

    } catch (Exception e) {
      LOG.error("failed to update job {} for yarn app {} ", jobId, yarnAppId);
    } finally {
      try {
        lock.release();
      } catch (Exception e) {
        LOG.error("fail releasing lock", e);
      }
    }
    return true;
  }
 @Override
 public void commitOffset(Subscription subscription, PartitionOffset partitionOffset)
     throws Exception {
   long firstToRead = partitionOffset.getOffset() + 1;
   byte[] data = String.valueOf(firstToRead).getBytes(Charset.forName("UTF-8"));
   String offsetPath =
       KafkaZookeeperPaths.partitionOffsetPath(
           kafkaNamesMapper.toConsumerGroupId(subscription),
           partitionOffset.getTopic(),
           partitionOffset.getPartition());
   try {
     curatorFramework.setData().forPath(offsetPath, data);
   } catch (KeeperException.NoNodeException ex) {
     curatorFramework.create().creatingParentsIfNeeded().forPath(offsetPath, data);
   }
 }
 public static void setDataAsyncWithCallback(CuratorFramework client, String path, byte[] payload)
     throws Exception {
   // this is another method of getting notification of an async completion
   client
       .setData()
       .inBackground(
           new BackgroundCallback() {
             @Override
             public void processResult(CuratorFramework client, CuratorEvent event)
                 throws Exception {
               if (event.getType() == CuratorEventType.CREATE) {
                 System.out.println("code:" + event.getResultCode() + "path:" + event.getPath());
               }
             }
           })
       .forPath(path, payload);
 }
  @Override
  public void saveData(String statePath, String data) {
    data = data == null ? "" : data;
    byte[] bytes = data.getBytes();

    try {
      if (curatorFramework.checkExists().forPath(statePath) == null) {
        curatorFramework.create().creatingParentsIfNeeded().forPath(statePath, bytes);
      } else {
        curatorFramework.setData().forPath(statePath, bytes);
      }

      logger.info(String.format("data was saved. path: %s, data: %s.", statePath, data));
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  private static void setValue(CuratorFramework client, String command, String[] args)
      throws Exception {
    if (args.length != 2) {
      System.err.println("syntax error (expected set <path> <value>): " + command);
      return;
    }

    String name = args[0];
    if (name.contains("/")) {
      System.err.println("Invalid node name" + name);
      return;
    }
    String path = ZKPaths.makePath(PATH, name);

    byte[] bytes = args[1].getBytes();
    try {
      client.setData().forPath(path, bytes);
    } catch (KeeperException.NoNodeException e) {
      client.create().creatingParentsIfNeeded().forPath(path, bytes);
    }
  }
 public static void setDataAsyncWithCallback(
     CuratorFramework client, BackgroundCallback callback, String path, byte[] payload)
     throws Exception {
   // this is another method of getting notification of an async completion
   client.setData().inBackground(callback).forPath(path, payload);
 }
 public static void setData(CuratorFramework client, String path, byte[] payload)
     throws Exception {
   client.setData().forPath(path, payload);
 }
Exemple #19
0
 public Stat setData(CuratorFramework zk, String path, byte[] data) throws Exception {
   String npath = PathUtils.normalize_path(path);
   return zk.setData().forPath(npath, data);
 }
Exemple #20
0
 void create(String path, String data) throws Exception {
   client.createContainers(path);
   client.setData().forPath(path, data.getBytes(Util.UTF_8));
   sleepABit();
 }