org.apache.cassandra.staleness.Staleness.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.cassandra.staleness.Staleness.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.cassandra.staleness;

import org.apache.cassandra.staleness.util.CassandraClient;

import org.apache.commons.cli.Option;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.log4j.PropertyConfigurator;
import org.apache.cassandra.utils.CT;

public final class Staleness {
    public static enum Operations {
        // Only INSERT and READ are used.
        INSERT, READ, RANGE_SLICE, INDEXED_RANGE_SLICE, MULTI_GET, COUNTER_ADD, COUNTER_GET
    }

    public static Session sessionInsert;
    public static Session sessionRead;
    public static Random randomizer = new Random();

    private static volatile boolean stopped = false;
    private static Logger logger = LoggerFactory.getLogger(Staleness.class);

    public static void main(String[] arguments) throws Exception {
        PropertyConfigurator.configure("/opt/apache-cassandra/tools/bin/log4j.properties");

        //logger.info("Hello");
        //CT.Log(logger, "Hello");

        try {
            sessionInsert = new SessionInsert(arguments);
            sessionRead = new SessionRead(arguments);
        } catch (IllegalArgumentException e) {
            printHelpMessage();
            return;
        }

        if (CassandraClient.Hostname().equals("cs30")) {
            // creating keyspace and column families
            sessionInsert.createKeySpaces();
        }
        // Let others sleep for 2 secs. Not a perfect synchronization though.
        else
            Thread.sleep(2000);

        PrintStream outStream = sessionInsert.getOutputStream();

        OperationLogger ol = new OperationLogger();
        ol.start();

        StalenessAction stalenessActionInsert = new StalenessAction(sessionInsert, outStream);
        StalenessAction stalenessActionRead = new StalenessAction(sessionRead, outStream);

        stalenessActionInsert.start();
        stalenessActionRead.start();

        stalenessActionInsert.join();
        stalenessActionRead.stopAction();
        stalenessActionRead.join();

        ol.Stop();
        ol.join();

        System.exit(stalenessActionInsert.getReturnCode() != 0 ? stalenessActionInsert.getReturnCode()
                : stalenessActionRead.getReturnCode());
    }

    /**
     * Printing out help message
     */
    public static void printHelpMessage() {
        System.out.println("Usage: ./bin/cassandra-staleness [options]\n\nOptions:");

        for (Object o : Session.availableOptions.getOptions()) {
            Option option = (Option) o;
            String upperCaseName = option.getLongOpt().toUpperCase();
            System.out.println(String.format("-%s%s, --%s%s%n\t\t%s%n", option.getOpt(),
                    (option.hasArg()) ? " " + upperCaseName : "", option.getLongOpt(),
                    (option.hasArg()) ? "=" + upperCaseName : "", option.getDescription()));
        }
    }

    // TODO: is this needed?
    private static class ShutDown extends Thread {
        private final Socket socket;
        private final ObjectOutputStream out;

        public ShutDown(Socket socket, ObjectOutputStream out) {
            this.out = out;
            this.socket = socket;
        }

        public void run() {
            try {
                if (!socket.isClosed()) {
                    System.out.println("Control-C caught. Canceling running action and shutting down...");

                    out.writeInt(1);
                    out.close();

                    stopped = true;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}