Exemplo n.º 1
0
  public static void main(String[] args) throws Exception {

    OptionParser parser = new OptionParser();
    parser
        .accepts("client-zone-id", "client zone id for zone routing")
        .withRequiredArg()
        .describedAs("zone-id")
        .ofType(Integer.class);
    OptionSet options = parser.parse(args);

    List<String> nonOptions = options.nonOptionArguments();
    if (nonOptions.size() < 2 || nonOptions.size() > 3) {
      System.err.println(
          "Usage: java VoldemortClientShell store_name bootstrap_url [command_file] [options]");
      parser.printHelpOn(System.err);
      System.exit(-1);
    }

    String storeName = nonOptions.get(0);
    String bootstrapUrl = nonOptions.get(1);

    String commandsFileName = "";
    BufferedReader fileReader = null;
    BufferedReader inputReader = null;
    try {
      if (nonOptions.size() == 3) {
        commandsFileName = nonOptions.get(2);
        fileReader = new BufferedReader(new FileReader(commandsFileName));
      }

      inputReader = new BufferedReader(new InputStreamReader(System.in));
    } catch (IOException e) {
      Utils.croak("Failure to open input stream: " + e.getMessage());
    }

    ClientConfig clientConfig =
        new ClientConfig()
            .setBootstrapUrls(bootstrapUrl)
            .setRequestFormatType(RequestFormatType.VOLDEMORT_V3);

    if (options.has("client-zone-id")) {
      clientConfig.setClientZoneId((Integer) options.valueOf("client-zone-id"));
    }

    StoreClientFactory factory = new SocketStoreClientFactory(clientConfig);

    try {
      client = (DefaultStoreClient<Object, Object>) factory.getStoreClient(storeName);
    } catch (Exception e) {
      Utils.croak("Could not connect to server: " + e.getMessage());
    }

    System.out.println("Established connection to " + storeName + " via " + bootstrapUrl);
    System.out.print(PROMPT);
    if (fileReader != null) {
      processCommands(factory, fileReader, true);
      fileReader.close();
    }
    processCommands(factory, inputReader, false);
  }
Exemplo n.º 2
0
 public SocketStore(String name, SocketDestination dest, SocketPool socketPool, boolean reroute) {
   this.name = Utils.notNull(name);
   this.pool = Utils.notNull(socketPool);
   this.destination = dest;
   this.requestFormat = requestFormatFactory.getRequestFormat(dest.getRequestFormatType());
   this.reroute = reroute;
 }
Exemplo n.º 3
0
  public static VoldemortConfig loadFromVoldemortHome(
      String voldemortHome, String voldemortConfigDir) {
    if (!Utils.isReadableDir(voldemortHome))
      throw new ConfigurationException(
          "Attempt to load configuration from VOLDEMORT_HOME, "
              + voldemortHome
              + " failed. That is not a readable directory.");

    if (voldemortConfigDir == null) {
      voldemortConfigDir = voldemortHome + File.separator + "config";
    }
    String propertiesFile = voldemortConfigDir + File.separator + "server.properties";
    if (!Utils.isReadableFile(propertiesFile))
      throw new ConfigurationException(propertiesFile + " is not a readable configuration file.");

    Props properties = null;
    try {
      properties = new Props(new File(propertiesFile));
      properties.put("voldemort.home", voldemortHome);
      properties.put("metadata.directory", voldemortConfigDir);
    } catch (IOException e) {
      throw new ConfigurationException(e);
    }

    return new VoldemortConfig(properties);
  }
Exemplo n.º 4
0
 public StatusServlet(VoldemortServer server, VelocityEngine engine) {
   this.server = Utils.notNull(server);
   this.velocityEngine = Utils.notNull(engine);
   this.abstractSocketService = (AbstractSocketService) server.getService(ServiceType.SOCKET);
   try {
     this.myMachine = InetAddress.getLocalHost().getHostName();
   } catch (UnknownHostException e) {
     myMachine = "unknown";
   }
 }
Exemplo n.º 5
0
 public SocketStore(
     String storeName,
     long timeoutMs,
     SocketDestination dest,
     ClientRequestExecutorPool pool,
     RequestRoutingType requestRoutingType) {
   this.storeName = Utils.notNull(storeName);
   this.timeoutMs = timeoutMs;
   this.pool = Utils.notNull(pool);
   this.destination = dest;
   this.requestFormat = requestFormatFactory.getRequestFormat(dest.getRequestFormatType());
   this.requestRoutingType = requestRoutingType;
 }
Exemplo n.º 6
0
 @Override
 public void init() throws ServletException {
   super.init();
   this.server =
       (VoldemortServer)
           Utils.notNull(
               getServletContext().getAttribute(VoldemortServletContextListener.SERVER_KEY));
   this.velocityEngine =
       (VelocityEngine)
           Utils.notNull(
               getServletContext()
                   .getAttribute(VoldemortServletContextListener.VELOCITY_ENGINE_KEY));
 }
Exemplo n.º 7
0
  public StreamingSlopPusherJob(
      StoreRepository storeRepo,
      MetadataStore metadataStore,
      FailureDetector failureDetector,
      VoldemortConfig voldemortConfig,
      ScanPermitWrapper repairPermits) {
    this.storeRepo = storeRepo;
    this.metadataStore = metadataStore;
    this.failureDetector = failureDetector;
    this.voldemortConfig = voldemortConfig;
    this.repairPermits = Utils.notNull(repairPermits);
    this.readThrottler = new EventThrottler(voldemortConfig.getSlopMaxReadBytesPerSec());
    this.adminClient = null;
    this.consumerResults = Lists.newArrayList();
    this.zoneMapping = Maps.newHashMap();
    this.consumerExecutor =
        Executors.newCachedThreadPool(
            new ThreadFactory() {

              public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("slop-pusher");
                return thread;
              }
            });
  }
Exemplo n.º 8
0
 public void initVelocity(ServletConfig config) {
   this.velocityEngine =
       (VelocityEngine)
           Utils.notNull(
               config
                   .getServletContext()
                   .getAttribute(VoldemortServletContextListener.VELOCITY_ENGINE_KEY));
 }
Exemplo n.º 9
0
 private List<ReadOnlyStorageEngine> getReadOnlyStores(VoldemortServer server) {
   StorageService storage = (StorageService) Utils.notNull(server).getService(ServiceType.STORAGE);
   List<ReadOnlyStorageEngine> l = Lists.newArrayList();
   for (StorageEngine<ByteArray, byte[]> engine :
       storage.getStoreRepository().getStorageEnginesByClass(ReadOnlyStorageEngine.class)) {
     l.add((ReadOnlyStorageEngine) engine);
   }
   return l;
 }
Exemplo n.º 10
0
  public BdbStorageEngine(String name, Environment environment, Database database) {
    this.name = Utils.notNull(name);
    this.bdbDatabase = Utils.notNull(database);
    this.environment = Utils.notNull(environment);
    this.versionedSerializer = new VersionedSerializer<byte[]>(new IdentitySerializer());
    this.versionSerializer =
        new Serializer<Version>() {

          public byte[] toBytes(Version object) {
            return ((VectorClock) object).toBytes();
          }

          public Version toObject(byte[] bytes) {
            return versionedSerializer.getVersion(bytes);
          }
        };
    this.isOpen = new AtomicBoolean(true);
  }
Exemplo n.º 11
0
 public static MetadataStore readFromDirectory(File dir, int nodeId) {
   if (!Utils.isReadableDir(dir))
     throw new IllegalArgumentException(
         "Metadata directory " + dir.getAbsolutePath() + " does not exist or can not be read.");
   if (dir.listFiles() == null)
     throw new IllegalArgumentException(
         "No configuration found in " + dir.getAbsolutePath() + ".");
   Store<String, String> innerStore =
       new ConfigurationStorageEngine(MetadataStore.METADATA_STORE_NAME, dir.getAbsolutePath());
   return new MetadataStore(innerStore, nodeId);
 }
Exemplo n.º 12
0
  /**
   * Schedule a data retention cleanup job for the given store
   *
   * @param storeDef The store definition
   * @param engine The storage engine to do cleanup on
   */
  private void scheduleCleanupJob(
      StoreDefinition storeDef, StorageEngine<ByteArray, byte[], byte[]> engine) {
    // Compute the start time of the job, based on current time
    GregorianCalendar cal =
        Utils.getCalendarForNextRun(
            new GregorianCalendar(),
            voldemortConfig.getRetentionCleanupFirstStartDayOfWeek(),
            voldemortConfig.getRetentionCleanupFirstStartTimeInHour());

    // allow only one cleanup job at a time
    Date startTime = cal.getTime();

    int maxReadRate =
        storeDef.hasRetentionScanThrottleRate()
            ? storeDef.getRetentionScanThrottleRate()
            : Integer.MAX_VALUE;

    logger.info(
        "Scheduling data retention cleanup job for store '"
            + storeDef.getName()
            + "' at "
            + startTime
            + " with retention scan throttle rate:"
            + maxReadRate
            + " Entries/second.");

    EventThrottler throttler = new EventThrottler(maxReadRate);

    Runnable cleanupJob =
        new DataCleanupJob<ByteArray, byte[], byte[]>(
            engine,
            scanPermitWrapper,
            storeDef.getRetentionDays() * Time.MS_PER_DAY,
            SystemTime.INSTANCE,
            throttler,
            metadata);
    if (voldemortConfig.isJmxEnabled()) {
      JmxUtils.registerMbean("DataCleanupJob-" + engine.getName(), cleanupJob);
    }

    long retentionFreqHours =
        storeDef.hasRetentionFrequencyDays()
            ? (storeDef.getRetentionFrequencyDays() * Time.HOURS_PER_DAY)
            : voldemortConfig.getRetentionCleanupScheduledPeriodInHour();

    this.scheduler.schedule(
        "cleanup-" + storeDef.getName(),
        cleanupJob,
        startTime,
        retentionFreqHours * Time.MS_PER_HOUR,
        voldemortConfig.getRetentionCleanupPinStartTime());
  }
Exemplo n.º 13
0
  private void doSwap(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    String dir = getRequired(req, "dir");
    String storeName = getRequired(req, "store");

    ReadOnlyStorageEngine store = this.getStore(storeName);
    if (store == null)
      throw new ServletException("'" + storeName + "' is not a registered read-only store.");

    if (!Utils.isReadableDir(dir))
      throw new ServletException("Store directory '" + dir + "' is not a readable directory.");

    store.swapFiles(dir);
    resp.getWriter().write("Swap completed.");
  }
Exemplo n.º 14
0
  /**
   * Retrieve the directory pointed by latest symbolic link
   *
   * @param parentDir The root directory
   * @return The directory pointed to by the latest symbolic link, else null
   */
  public static File getLatestDir(File parentDir) {
    File latestSymLink = new File(parentDir, "latest");

    if (latestSymLink.exists() && Utils.isSymLink(latestSymLink)) {
      File canonicalLatestVersion = null;
      try {
        canonicalLatestVersion = latestSymLink.getCanonicalFile();
      } catch (IOException e) {
        return null;
      }

      if (canonicalLatestVersion != null && checkVersionDirName(canonicalLatestVersion))
        return canonicalLatestVersion;
    }
    return null;
  }
Exemplo n.º 15
0
  public static VoldemortConfig loadFromEnvironmentVariable() {
    String voldemortHome = System.getenv(VoldemortConfig.VOLDEMORT_HOME_VAR_NAME);
    if (voldemortHome == null)
      throw new ConfigurationException(
          "No environment variable "
              + VoldemortConfig.VOLDEMORT_HOME_VAR_NAME
              + " has been defined, set it!");

    String voldemortConfigDir = System.getenv(VoldemortConfig.VOLDEMORT_CONFIG_DIR);
    if (voldemortConfigDir != null) {
      if (!Utils.isReadableDir(voldemortConfigDir))
        throw new ConfigurationException(
            "Attempt to load configuration from VOLDEMORT_CONFIG_DIR, "
                + voldemortConfigDir
                + " failed. That is not a readable directory.");
    }
    return loadFromVoldemortHome(voldemortHome, voldemortConfigDir);
  }
Exemplo n.º 16
0
  protected RoutedStore(
      String name,
      Map<Integer, Store<ByteArray, byte[], byte[]>> innerStores,
      Cluster cluster,
      StoreDefinition storeDef,
      boolean repairReads,
      TimeoutConfig timeoutConfig,
      FailureDetector failureDetector,
      Time time) {
    if (storeDef.getRequiredReads() < 1)
      throw new IllegalArgumentException(
          "Cannot have a storeDef.getRequiredReads() number less than 1.");
    if (storeDef.getRequiredWrites() < 1)
      throw new IllegalArgumentException(
          "Cannot have a storeDef.getRequiredWrites() number less than 1.");
    if (storeDef.getPreferredReads() < storeDef.getRequiredReads())
      throw new IllegalArgumentException(
          "storeDef.getPreferredReads() must be greater or equal to storeDef.getRequiredReads().");
    if (storeDef.getPreferredWrites() < storeDef.getRequiredWrites())
      throw new IllegalArgumentException(
          "storeDef.getPreferredWrites() must be greater or equal to storeDef.getRequiredWrites().");
    if (storeDef.getPreferredReads() > innerStores.size())
      throw new IllegalArgumentException(
          "storeDef.getPreferredReads() is larger than the total number of nodes!");
    if (storeDef.getPreferredWrites() > innerStores.size())
      throw new IllegalArgumentException(
          "storeDef.getPreferredWrites() is larger than the total number of nodes!");

    this.name = name;
    this.innerStores =
        new ConcurrentHashMap<Integer, Store<ByteArray, byte[], byte[]>>(innerStores);
    this.repairReads = repairReads;
    this.readRepairer = new ReadRepairer<ByteArray, byte[]>();
    this.timeoutConfig = timeoutConfig;
    this.time = Utils.notNull(time);
    this.storeDef = storeDef;
    this.failureDetector = failureDetector;
    this.routingStrategy = new RoutingStrategyFactory().updateRoutingStrategy(storeDef, cluster);
  }
Exemplo n.º 17
0
  public StreamingSlopPusherJob(
      StoreRepository storeRepo,
      MetadataStore metadataStore,
      FailureDetector failureDetector,
      VoldemortConfig voldemortConfig,
      Semaphore repairPermits) {
    this.storeRepo = storeRepo;
    this.metadataStore = metadataStore;
    this.failureDetector = failureDetector;
    this.voldemortConfig = voldemortConfig;
    this.repairPermits = Utils.notNull(repairPermits);

    this.cluster = metadataStore.getCluster();
    this.slopQueues =
        new ConcurrentHashMap<Integer, SynchronousQueue<Versioned<Slop>>>(
            cluster.getNumberOfNodes());
    this.consumerExecutor =
        Executors.newFixedThreadPool(
            cluster.getNumberOfNodes(),
            new ThreadFactory() {

              public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("slop-pusher");
                return thread;
              }
            });

    this.readThrottler = new EventThrottler(voldemortConfig.getSlopMaxReadBytesPerSec());
    this.adminClient = null;
    this.consumerResults = Lists.newArrayList();
    this.attemptedByNode = new ConcurrentHashMap<Integer, Long>(cluster.getNumberOfNodes());
    this.succeededByNode = new ConcurrentHashMap<Integer, Long>(cluster.getNumberOfNodes());

    this.zoneMapping = Maps.newHashMap();
  }
Exemplo n.º 18
0
 public FailureDetectorConfig setTime(Time time) {
   this.time = Utils.notNull(time);
   return this;
 }
Exemplo n.º 19
0
 public FailureDetectorConfig setStoreVerifier(StoreVerifier storeVerifier) {
   this.storeVerifier = Utils.notNull(storeVerifier);
   return this;
 }
Exemplo n.º 20
0
 public synchronized void removeNode(Node node) {
   Utils.notNull(node);
   nodes.remove(node);
 }
Exemplo n.º 21
0
 public synchronized void addNode(Node node) {
   Utils.notNull(node);
   nodes.add(node);
 }
Exemplo n.º 22
0
 /**
  * Assigns a list of nodes in the cluster represented by this failure detector configuration.
  *
  * @param nodes Collection of Node instances, usually determined from the Cluster; must be
  *     non-null
  */
 @Deprecated
 public synchronized FailureDetectorConfig setNodes(Collection<Node> nodes) {
   Utils.notNull(nodes);
   this.nodes = new HashSet<Node>(nodes);
   return this;
 }
Exemplo n.º 23
0
 /**
  * Assigns a cluster which determines the source of truth for the topology
  *
  * @param cluster The Cluster object retrieved during bootstrap; must be non-null
  */
 public FailureDetectorConfig setCluster(Cluster cluster) {
   Utils.notNull(cluster);
   this.cluster = cluster;
   return this;
 }
Exemplo n.º 24
0
 /**
  * Assigns the list of Java Exception types that are considered catastrophic. Some FailureDetector
  * implementations may not mark a given node as unavailable on each and every call to
  * recordException. Instead they may apply logic to determine if such an exception should cause
  * the node to be marked as unavailable. However, the list of so-called catastrophic errors
  * provides such FailureDetector implementations a hint that receipt of such errors should cause
  * the node to be marked as unavailable immediately, regardless of other logic.
  *
  * <p><b>Note</b>: this is only used by the {@link ThresholdFailureDetector} implementation.
  *
  * @param catastrophicErrorTypes List of fully-qualified Java Exception class names against which
  *     to check the exception provided to recordException; this list should be immutable and
  *     non-null
  * @see ThresholdFailureDetector
  * @see VoldemortConfig#getFailureDetectorCatastrophicErrorTypes
  * @see ClientConfig#getFailureDetectorCatastrophicErrorTypes
  */
 public FailureDetectorConfig setCatastrophicErrorTypes(List<String> catastrophicErrorTypes) {
   this.catastrophicErrorTypes = Utils.notNull(catastrophicErrorTypes);
   return this;
 }
Exemplo n.º 25
0
 /**
  * Assigns the fully-qualified class name of the FailureDetector implementation.
  *
  * @param implementationClassName Class name to instantiate for the FailureDetector
  * @see VoldemortConfig#getFailureDetectorImplementation
  * @see ClientConfig#getFailureDetectorImplementation
  */
 public FailureDetectorConfig setImplementationClassName(String implementationClassName) {
   this.implementationClassName = Utils.notNull(implementationClassName);
   return this;
 }
Exemplo n.º 26
0
 /**
  * Set the tier at which routing occurs. Client-side routing occurs on the client, and server-side
  * routing on the server.
  *
  * <p>NOTE : Server side routing is not used, as yet. The java client only supports client-side
  * routing.
  *
  * @param routingTier The routing tier to use for routing requests
  */
 public ClientConfig setRoutingTier(RoutingTier routingTier) {
   this.routingTier = Utils.notNull(routingTier);
   return this;
 }
Exemplo n.º 27
0
 /**
  * Set the request format type used for network communications (for example protocol buffers)
  *
  * @param requestFormatType The type of the network protocol
  */
 public ClientConfig setRequestFormatType(RequestFormatType requestFormatType) {
   this.requestFormatType = Utils.notNull(requestFormatType);
   return this;
 }
Exemplo n.º 28
0
 /**
  * Set the bootstrap urls from which to attempt a connection
  *
  * @param bootstrapUrls The urls to bootstrap from
  */
 public ClientConfig setBootstrapUrls(String... bootstrapUrls) {
   this.bootstrapUrls = Arrays.asList(Utils.notNull(bootstrapUrls));
   if (this.bootstrapUrls.size() <= 0)
     throw new IllegalArgumentException("Must provide at least one bootstrap URL.");
   return this;
 }
Exemplo n.º 29
0
 /** Set the SerializerFactory used to serialize and deserialize values */
 public ClientConfig setSerializerFactory(SerializerFactory serializerFactory) {
   this.serializerFactory = Utils.notNull(serializerFactory);
   return this;
 }
Exemplo n.º 30
0
  /**
   * This method handles submitting and then waiting for the request from the server. It uses the
   * ClientRequest API to actually write the request and then read back the response. This
   * implementation will block for a response from the server.
   *
   * @param <T> Return type
   * @param clientRequest ClientRequest implementation used to write the request and read the
   *     response
   * @param operationName Simple string representing the type of request
   * @return Data returned by the individual requests
   */
  private <T> T request(ClientRequest<T> delegate, String operationName) {
    long startTimeMs = -1;
    long startTimeNs = -1;

    if (logger.isDebugEnabled()) {
      startTimeMs = System.currentTimeMillis();
    }
    ClientRequestExecutor clientRequestExecutor = pool.checkout(destination);
    String debugMsgStr = "";

    startTimeNs = System.nanoTime();

    BlockingClientRequest<T> blockingClientRequest = null;
    try {
      blockingClientRequest = new BlockingClientRequest<T>(delegate, timeoutMs);
      clientRequestExecutor.addClientRequest(
          blockingClientRequest, timeoutMs, System.nanoTime() - startTimeNs);

      boolean awaitResult = blockingClientRequest.await();

      if (awaitResult == false) {
        blockingClientRequest.timeOut();
      }

      if (logger.isDebugEnabled()) debugMsgStr += "success";

      return blockingClientRequest.getResult();
    } catch (InterruptedException e) {

      if (logger.isDebugEnabled()) debugMsgStr += "unreachable: " + e.getMessage();

      throw new UnreachableStoreException(
          "Failure in " + operationName + " on " + destination + ": " + e.getMessage(), e);
    } catch (UnreachableStoreException e) {
      clientRequestExecutor.close();

      if (logger.isDebugEnabled()) debugMsgStr += "failure: " + e.getMessage();

      throw new UnreachableStoreException(
          "Failure in " + operationName + " on " + destination + ": " + e.getMessage(),
          e.getCause());
    } finally {
      if (blockingClientRequest != null && !blockingClientRequest.isComplete()) {
        // close the executor if we timed out
        clientRequestExecutor.close();
      }
      // Record operation time
      long opTimeNs = Utils.elapsedTimeNs(startTimeNs, System.nanoTime());
      if (stats != null) {
        stats.recordSyncOpTimeNs(destination, opTimeNs);
      }
      if (logger.isDebugEnabled()) {
        logger.debug(
            "Sync request end, type: "
                + operationName
                + " requestRef: "
                + System.identityHashCode(delegate)
                + " totalTimeNs: "
                + opTimeNs
                + " start time: "
                + startTimeMs
                + " end time: "
                + System.currentTimeMillis()
                + " client:"
                + clientRequestExecutor.getSocketChannel().socket().getLocalAddress()
                + ":"
                + clientRequestExecutor.getSocketChannel().socket().getLocalPort()
                + " server: "
                + clientRequestExecutor.getSocketChannel().socket().getRemoteSocketAddress()
                + " outcome: "
                + debugMsgStr);
      }

      pool.checkin(destination, clientRequestExecutor);
    }
  }