Exemplo n.º 1
0
  private void parseOptions(String[] args) {

    if (args.length == 0) {
      printHelp();
      System.exit(0);
    }

    try {
      line = parser.parse(getOptions(), args);

      if (line.hasOption("start") && line.hasOption("kill")) {
        throw new ParseException("Cannot simultaneously start and kill a cache");
      }

      // verify all required options are there
      if (line.hasOption("start")
          && (!line.hasOption("port") || !line.hasOption("name") || !line.hasOption("type"))) {
        throw new ParseException("Cannot start cache without port, name, and type params");
      }
    } catch (MissingOptionException e) {
      System.err.println(e.getMessage());
      printHelp();
      System.exit(-1);
    } catch (ParseException e) {
      e.printStackTrace();
      System.exit(-1);
    }

    if (line.hasOption("help")) {
      printHelp();
      System.exit(0);
    }

    if (line.hasOption("start")) {
      cacheName = line.getOptionValue("name");
      type = CacheType.valueOf(line.getOptionValue("type").toUpperCase());
      port = Integer.parseInt(line.getOptionValue("port"));

      if (type == null) {
        LOG.error("Invalid type.");
        printHelp();
        System.exit(-1);
      }
    }
  }
Exemplo n.º 2
0
  private void startCache() throws Exception {

    LOG.info("Starting Nimbus cache");
    Nimbus.getZooKeeper();
    LOG.info("ZooKeeper finished");

    // Set the Cache ZNode to the root + the Cache name
    CACHE_ZNODE = ROOT_ZNODE + "/" + cacheName;
    String cacheletName = InetAddress.getLocalHost().getHostName();
    CACHELET_ZNODE = CACHE_ZNODE + "/" + cacheletName;
    knownServers.put(InetAddress.getLocalHost().getHostName(), null);

    // add shutdown hook
    NimbusShutdownHook.createInstance(type);
    LOG.info("making root node");
    // ensure this root path exists
    getZooKeeper().ensurePaths(ROOT_ZNODE);

    LOG.info("done making root node");
    if (type.equals(CacheType.MASTER)) {
      createMasterCacheInfo();
    } else {
      // this info is for a non-master cache
      info = NimbusMaster.getInstance().getCacheInfo(cacheName);
      if (info == null) {
        throw new RuntimeException("No info for " + cacheName);
      }
    }

    // create my Cachelet
    switch (info.getType()) {
      case STATIC_SET:
        cachelet =
            new StaticSetCacheletServer(
                info.getName(), cacheletName, info.getPort(), info.getType());
        break;
      case DYNAMIC_SET:
        cachelet =
            new DynamicSetCacheletServer(
                info.getName(), cacheletName, info.getPort(), info.getType());
        break;
      case MASTER:
        cachelet =
            new MasterCacheletServer(info.getName(), cacheletName, info.getPort(), info.getType());
        break;
      case MAPSET:
        cachelet =
            new MapSetCacheletServer(info.getName(), cacheletName, info.getPort(), info.getType());
        break;

      case DYNAMIC_MAP:
        cachelet =
            new DynamicMapCacheletServer(
                info.getName(), cacheletName, info.getPort(), info.getType());
        break;
      default:
        LOG.error("Unkown type " + info.getType().toString() + ". Shutting down");
        System.exit(0);
    }

    Thread t = new Thread(cachelet);
    t.start();

    // add myself to ZooKeeper
    LOG.info("Creating my ZNode at " + CACHELET_ZNODE);
    getZooKeeper().ensurePaths(CACHELET_ZNODE);

    if (NimbusConf.getConf().isSafetyNetEnabled()) {
      // this while loop manages connections to other Cachelets
      // if a Cachelet connects, then create a new thread to handle
      // communication to that Cachelet and wait for more connections
      LOG.info("Starting heartbeat cycle...");
      long hbInterval = NimbusConf.getConf().getCacheletHeartbeatInterval();
      getZooKeeper().setDataVariable(CACHELET_ZNODE, BytesUtil.EMPTY_BYTES);
      while (!false) {
        Thread.sleep(hbInterval);
        getZooKeeper().setDataVariable(CACHELET_ZNODE, BytesUtil.EMPTY_BYTES);
      }
    } else {
      LOG.info("Safety net is disabled... simply sleeping this thread.");
      while (!false) {
        Thread.sleep(Integer.MAX_VALUE);
      }
    }
  }