Example usage for java.util.concurrent.atomic AtomicInteger get

List of usage examples for java.util.concurrent.atomic AtomicInteger get

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger get.

Prototype

public final int get() 

Source Link

Document

Returns the current value, with memory effects as specified by VarHandle#getVolatile .

Usage

From source file:com.linkedin.pinotdruidbenchmark.DruidThroughput.java

@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws Exception {
    if (args.length != 3 && args.length != 4) {
        System.err.println(//from  w w w  .j a va2 s  .  c o  m
                "3 or 4 arguments required: QUERY_DIR, RESOURCE_URL, NUM_CLIENTS, TEST_TIME (seconds).");
        return;
    }

    File queryDir = new File(args[0]);
    String resourceUrl = args[1];
    final int numClients = Integer.parseInt(args[2]);
    final long endTime;
    if (args.length == 3) {
        endTime = Long.MAX_VALUE;
    } else {
        endTime = System.currentTimeMillis() + Integer.parseInt(args[3]) * MILLIS_PER_SECOND;
    }

    File[] queryFiles = queryDir.listFiles();
    assert queryFiles != null;
    Arrays.sort(queryFiles);

    final int numQueries = queryFiles.length;
    final HttpPost[] httpPosts = new HttpPost[numQueries];
    for (int i = 0; i < numQueries; i++) {
        HttpPost httpPost = new HttpPost(resourceUrl);
        httpPost.addHeader("content-type", "application/json");
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(queryFiles[i]))) {
            int length;
            while ((length = bufferedReader.read(CHAR_BUFFER)) > 0) {
                stringBuilder.append(new String(CHAR_BUFFER, 0, length));
            }
        }
        String query = stringBuilder.toString();
        httpPost.setEntity(new StringEntity(query));
        httpPosts[i] = httpPost;
    }

    final AtomicInteger counter = new AtomicInteger(0);
    final AtomicLong totalResponseTime = new AtomicLong(0L);
    final ExecutorService executorService = Executors.newFixedThreadPool(numClients);

    for (int i = 0; i < numClients; i++) {
        executorService.submit(new Runnable() {
            @Override
            public void run() {
                try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
                    while (System.currentTimeMillis() < endTime) {
                        long startTime = System.currentTimeMillis();
                        CloseableHttpResponse httpResponse = httpClient
                                .execute(httpPosts[RANDOM.nextInt(numQueries)]);
                        httpResponse.close();
                        long responseTime = System.currentTimeMillis() - startTime;
                        counter.getAndIncrement();
                        totalResponseTime.getAndAdd(responseTime);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    executorService.shutdown();

    long startTime = System.currentTimeMillis();
    while (System.currentTimeMillis() < endTime) {
        Thread.sleep(REPORT_INTERVAL_MILLIS);
        double timePassedSeconds = ((double) (System.currentTimeMillis() - startTime)) / MILLIS_PER_SECOND;
        int count = counter.get();
        double avgResponseTime = ((double) totalResponseTime.get()) / count;
        System.out.println("Time Passed: " + timePassedSeconds + "s, Query Executed: " + count + ", QPS: "
                + count / timePassedSeconds + ", Avg Response Time: " + avgResponseTime + "ms");
    }
}

From source file:edu.msu.cme.rdp.kmer.KmerFilter.java

public static void main(String[] args) throws Exception {
    final KmerTrie kmerTrie;
    final SeqReader queryReader;
    final SequenceType querySeqType;
    final File queryFile;
    final KmerStartsWriter out;
    final boolean translQuery;
    final int wordSize;
    final int translTable;
    final boolean alignedSeqs;
    final List<String> refLabels = new ArrayList();
    final int maxThreads;

    try {/*w w  w  . j av  a 2s.c  o  m*/
        CommandLine cmdLine = new PosixParser().parse(options, args);
        args = cmdLine.getArgs();

        if (args.length < 3) {
            throw new Exception("Unexpected number of arguments");
        }

        if (cmdLine.hasOption("out")) {
            out = new KmerStartsWriter(cmdLine.getOptionValue("out"));
        } else {
            out = new KmerStartsWriter(System.out);
        }

        if (cmdLine.hasOption("aligned")) {
            alignedSeqs = true;
        } else {
            alignedSeqs = false;
        }

        if (cmdLine.hasOption("transl-table")) {
            translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table"));
        } else {
            translTable = 11;
        }

        if (cmdLine.hasOption("threads")) {
            maxThreads = Integer.valueOf(cmdLine.getOptionValue("threads"));
        } else {
            maxThreads = Runtime.getRuntime().availableProcessors();
        }

        queryFile = new File(args[1]);
        wordSize = Integer.valueOf(args[0]);
        SequenceType refSeqType = null;

        querySeqType = SeqUtils.guessSequenceType(queryFile);
        queryReader = new SequenceReader(queryFile);

        if (querySeqType == SequenceType.Protein) {
            throw new Exception("Expected nucl query sequences");
        }

        refSeqType = SeqUtils
                .guessSequenceType(new File(args[2].contains("=") ? args[2].split("=")[1] : args[2]));

        translQuery = refSeqType == SequenceType.Protein;

        if (translQuery && wordSize % 3 != 0) {
            throw new Exception("Word size must be a multiple of 3 for nucl ref seqs");
        }

        int trieWordSize;
        if (translQuery) {
            trieWordSize = wordSize / 3;
        } else {
            trieWordSize = wordSize;
        }
        kmerTrie = new KmerTrie(trieWordSize, translQuery);

        for (int index = 2; index < args.length; index++) {
            String refName;
            String refFileName = args[index];
            if (refFileName.contains("=")) {
                String[] lexemes = refFileName.split("=");
                refName = lexemes[0];
                refFileName = lexemes[1];
            } else {
                String tmpName = new File(refFileName).getName();
                if (tmpName.contains(".")) {
                    refName = tmpName.substring(0, tmpName.lastIndexOf("."));
                } else {
                    refName = tmpName;
                }
            }

            File refFile = new File(refFileName);

            if (refSeqType != SeqUtils.guessSequenceType(refFile)) {
                throw new Exception(
                        "Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile)
                                + " sequences but expected " + refSeqType + " sequences");
            }

            SequenceReader seqReader = new SequenceReader(refFile);
            Sequence seq;

            while ((seq = seqReader.readNextSequence()) != null) {
                if (seq.getSeqName().startsWith("#")) {
                    continue;
                }
                if (alignedSeqs) {
                    kmerTrie.addModelSequence(seq, refLabels.size());
                } else {
                    kmerTrie.addSequence(seq, refLabels.size());
                }
            }
            seqReader.close();

            refLabels.add(refName);
        }

    } catch (Exception e) {
        new HelpFormatter().printHelp("KmerSearch <word_size> <query_file> [name=]<ref_file> ...", options);
        System.err.println(e.getMessage());
        e.printStackTrace();
        System.exit(1);
        throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables
    }

    long startTime = System.currentTimeMillis();
    long seqCount = 0;
    final int maxTasks = 25000;

    /*
     * if (args.length == 4) { maxThreads = Integer.valueOf(args[3]); } else {
     */

    //}

    System.err.println("Starting kmer mapping at " + new Date());
    System.err.println("*  Number of threads:       " + maxThreads);
    System.err.println("*  References:              " + refLabels);
    System.err.println("*  Reads file:              " + queryFile);
    System.err.println("*  Kmer length:             " + kmerTrie.getWordSize());

    final AtomicInteger processed = new AtomicInteger();
    final AtomicInteger outstandingTasks = new AtomicInteger();

    ExecutorService service = Executors.newFixedThreadPool(maxThreads);

    Sequence querySeq;

    while ((querySeq = queryReader.readNextSequence()) != null) {
        seqCount++;

        String seqString = querySeq.getSeqString();

        if (seqString.length() < 3) {
            System.err.println("Sequence " + querySeq.getSeqName() + "'s length is less than 3");
            continue;
        }

        final Sequence threadSeq = querySeq;

        Runnable r = new Runnable() {

            public void run() {
                try {
                    processSeq(threadSeq, refLabels, kmerTrie, out, wordSize, translQuery, translTable, false);
                    processSeq(threadSeq, refLabels, kmerTrie, out, wordSize, translQuery, translTable, true);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

                processed.incrementAndGet();
                outstandingTasks.decrementAndGet();
            }
        };

        outstandingTasks.incrementAndGet();
        service.submit(r);

        while (outstandingTasks.get() >= maxTasks)
            ;

        if ((processed.get() + 1) % 1000000 == 0) {
            System.err.println("Processed " + processed + " sequences in "
                    + (System.currentTimeMillis() - startTime) + " ms");
        }
    }

    service.shutdown();
    service.awaitTermination(1, TimeUnit.DAYS);

    System.err.println("Finished Processed " + processed + " sequences in "
            + (System.currentTimeMillis() - startTime) + " ms");

    out.close();
}

From source file:edu.msu.cme.rdp.kmer.cli.FastKmerFilter.java

public static void main(String[] args) throws Exception {
    final KmerSet<Set<RefKmer>> kmerSet;
    final SeqReader queryReader;
    final SequenceType querySeqType;
    final File queryFile;
    final KmerStartsWriter out;
    final boolean translQuery;
    final int wordSize;
    final int translTable;
    final boolean alignedSeqs;
    final List<String> refLabels = new ArrayList();
    final int maxThreads;
    final int trieWordSize;

    try {/* w w  w.j av a  2s. c o m*/
        CommandLine cmdLine = new PosixParser().parse(options, args);
        args = cmdLine.getArgs();

        if (args.length < 3) {
            throw new Exception("Unexpected number of arguments");
        }

        if (cmdLine.hasOption("out")) {
            out = new KmerStartsWriter(cmdLine.getOptionValue("out"));
        } else {
            out = new KmerStartsWriter(System.out);
        }

        if (cmdLine.hasOption("aligned")) {
            alignedSeqs = true;
        } else {
            alignedSeqs = false;
        }

        if (cmdLine.hasOption("transl-table")) {
            translTable = Integer.valueOf(cmdLine.getOptionValue("transl-table"));
        } else {
            translTable = 11;
        }

        if (cmdLine.hasOption("threads")) {
            maxThreads = Integer.valueOf(cmdLine.getOptionValue("threads"));
        } else {
            maxThreads = Runtime.getRuntime().availableProcessors();
        }

        queryFile = new File(args[1]);
        wordSize = Integer.valueOf(args[0]);
        SequenceType refSeqType = null;

        querySeqType = SeqUtils.guessSequenceType(queryFile);
        queryReader = new SequenceReader(queryFile);

        if (querySeqType == SequenceType.Protein) {
            throw new Exception("Expected nucl query sequences");
        }

        refSeqType = SeqUtils
                .guessSequenceType(new File(args[2].contains("=") ? args[2].split("=")[1] : args[2]));

        translQuery = refSeqType == SequenceType.Protein;

        if (translQuery && wordSize % 3 != 0) {
            throw new Exception("Word size must be a multiple of 3 for nucl ref seqs");
        }

        if (translQuery) {
            trieWordSize = wordSize / 3;
        } else {
            trieWordSize = wordSize;
        }
        kmerSet = new KmerSet<Set<RefKmer>>();//new KmerTrie(trieWordSize, translQuery);

        for (int index = 2; index < args.length; index++) {
            String refName;
            String refFileName = args[index];
            if (refFileName.contains("=")) {
                String[] lexemes = refFileName.split("=");
                refName = lexemes[0];
                refFileName = lexemes[1];
            } else {
                String tmpName = new File(refFileName).getName();
                if (tmpName.contains(".")) {
                    refName = tmpName.substring(0, tmpName.lastIndexOf("."));
                } else {
                    refName = tmpName;
                }
            }

            File refFile = new File(refFileName);

            if (refSeqType != SeqUtils.guessSequenceType(refFile)) {
                throw new Exception(
                        "Reference file " + refFile + " contains " + SeqUtils.guessFileFormat(refFile)
                                + " sequences but expected " + refSeqType + " sequences");
            }

            SequenceReader seqReader = new SequenceReader(refFile);
            Sequence seq;

            while ((seq = seqReader.readNextSequence()) != null) {
                if (seq.getSeqName().startsWith("#")) {
                    continue;
                }

                KmerGenerator kmers;
                try {
                    if (translQuery) { //protein ref
                        kmers = new ProtKmerGenerator(seq.getSeqString(), trieWordSize, alignedSeqs);
                    } else {
                        kmers = new NuclKmerGenerator(seq.getSeqString(), trieWordSize, alignedSeqs);
                    }
                    while (kmers.hasNext()) {
                        Kmer temp = kmers.next();
                        long[] next = temp.getLongKmers();
                        Set<RefKmer> refKmers = kmerSet.get(next);
                        if (refKmers == null) {
                            refKmers = new HashSet();
                            kmerSet.add(next, refKmers);
                        }

                        RefKmer kmerRef = new RefKmer();
                        kmerRef.modelPos = kmers.getPosition();
                        kmerRef.refFileIndex = refLabels.size();
                        kmerRef.refSeqid = seq.getSeqName();
                        refKmers.add(kmerRef);
                    }
                } catch (IllegalArgumentException ex) {
                    //System.err.println(seq.getSeqName()+ " " + ex.getMessage());
                }
            }
            seqReader.close();

            refLabels.add(refName);
        }

    } catch (Exception e) {
        new HelpFormatter().printHelp(
                "KmerSearch <kmerSize> <query_file> [name=]<ref_file> ...\nkmerSize should be multiple of 3, (recommend 45, minimum 30, maximum 63) ",
                options);
        e.printStackTrace();
        System.exit(1);
        throw new RuntimeException("Stupid jvm"); //While this will never get thrown it is required to make sure javac doesn't get confused about uninitialized variables
    }

    long startTime = System.currentTimeMillis();
    long seqCount = 0;
    final int maxTasks = 25000;

    System.err.println("Starting kmer mapping at " + new Date());
    System.err.println("*  Number of threads:       " + maxThreads);
    System.err.println("*  References:              " + refLabels);
    System.err.println("*  Reads file:              " + queryFile);
    System.err.println("*  Kmer length:             " + trieWordSize);
    System.err.println("*  Kmer Refset Size:        " + kmerSet.size());

    final AtomicInteger processed = new AtomicInteger();
    final AtomicInteger outstandingTasks = new AtomicInteger();

    ExecutorService service = Executors.newFixedThreadPool(maxThreads);

    Sequence querySeq;

    while ((querySeq = queryReader.readNextSequence()) != null) {
        seqCount++;

        String seqString = querySeq.getSeqString();

        if ((!translQuery && seqString.length() < wordSize)
                || (translQuery && seqString.length() < wordSize + 2)) {
            //System.err.println(querySeq.getSeqName() + "\t" + seqString.length());
            continue;
        }

        final Sequence threadSeq = querySeq;

        Runnable r = new Runnable() {

            public void run() {
                try {
                    processSeq(threadSeq, refLabels, kmerSet, out, wordSize, translQuery, translTable, false);
                    processSeq(threadSeq, refLabels, kmerSet, out, wordSize, translQuery, translTable, true);

                    processed.incrementAndGet();
                    outstandingTasks.decrementAndGet();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        outstandingTasks.incrementAndGet();
        service.submit(r);

        while (outstandingTasks.get() >= maxTasks)
            ;

        if ((processed.get() + 1) % 1000000 == 0) {
            System.err.println("Processed " + processed + " sequences in "
                    + (System.currentTimeMillis() - startTime) + " ms");
        }
    }

    service.shutdown();
    service.awaitTermination(1, TimeUnit.DAYS);

    System.err.println("Finished Processed " + processed + " sequences in "
            + (System.currentTimeMillis() - startTime) + " ms");

    out.close();
}

From source file:DruidThroughput.java

@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) throws Exception {
    final int numQueries = QUERIES.length;
    final Random random = new Random(RANDOM_SEED);
    final AtomicInteger counter = new AtomicInteger(0);
    final AtomicLong totalResponseTime = new AtomicLong(0L);
    final ExecutorService executorService = Executors.newFixedThreadPool(NUM_CLIENTS);

    for (int i = 0; i < NUM_CLIENTS; i++) {
        executorService.submit(new Runnable() {
            @Override/*from  ww  w  .  j a  va2  s. c  o m*/
            public void run() {
                try (CloseableHttpClient client = HttpClients.createDefault()) {
                    HttpPost post = new HttpPost("http://localhost:8082/druid/v2/?pretty");
                    post.addHeader("content-type", "application/json");
                    CloseableHttpResponse res;
                    while (true) {
                        try (BufferedReader reader = new BufferedReader(new FileReader(
                                QUERY_FILE_DIR + File.separator + random.nextInt(numQueries) + ".json"))) {
                            int length = reader.read(BUFFER);
                            post.setEntity(new StringEntity(new String(BUFFER, 0, length)));
                        }
                        long start = System.currentTimeMillis();
                        res = client.execute(post);
                        res.close();
                        counter.getAndIncrement();
                        totalResponseTime.getAndAdd(System.currentTimeMillis() - start);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    long startTime = System.currentTimeMillis();
    while (true) {
        Thread.sleep(REPORT_INTERVAL_MILLIS);
        double timePassedSeconds = ((double) (System.currentTimeMillis() - startTime)) / MILLIS_PER_SECOND;
        int count = counter.get();
        double avgResponseTime = ((double) totalResponseTime.get()) / count;
        System.out.println("Time Passed: " + timePassedSeconds + "s, Query Executed: " + count + ", QPS: "
                + count / timePassedSeconds + ", Avg Response Time: " + avgResponseTime + "ms");
    }
}

From source file:org.apache.hadoop.hbase.test.MultiThreadedMultiClusterWithCombinedFileTest.java

public static void main(String[] args) throws Exception {
    if (args.length == 0) {

        System.out.println("RunMultiClusterTest " + "<combined file> " + "<tableName> " + "<familyName> "
                + "<numberOfPuts> " + "<millisecond of wait> " + "<numberOfThreads> " + "<outputCsvFile>");
    }//  ww w .ja  v a  2  s .c om

    final String combinedFilePath = args[0];

    System.out.println("--Getting Configurations");

    Configuration config = HBaseConfiguration.create();
    config.addResource(new FileInputStream(combinedFilePath));

    System.out.println("--Got Configuration");

    final String tableName = args[1];
    final String familyName = args[2];
    final int numberOfPuts = Integer.parseInt(args[3]);
    final int millisecondToWait = Integer.parseInt(args[4]);
    final int numberOfThreads = Integer.parseInt(args[5]);
    final String outputCsvFile = args[6];

    System.out.println("Getting HAdmin");

    System.out.println(ConfigConst.HBASE_FAILOVER_CLUSTERS_CONFIG + ": "
            + config.get(ConfigConst.HBASE_FAILOVER_CLUSTERS_CONFIG));
    System.out.println("hbase.zookeeper.quorum: " + config.get("hbase.zookeeper.quorum"));
    System.out.println("hbase.failover.cluster.fail1.hbase.hstore.compaction.max: "
            + config.get("hbase.failover.cluster.fail1.hbase.hstore.compaction.max"));

    HBaseAdmin admin = new HBaseAdminMultiCluster(config);

    try {
        admin.disableTable(TableName.valueOf(tableName));
        admin.deleteTable(TableName.valueOf(tableName));
    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println(" - Got HAdmin:" + admin.getClass());

    HTableDescriptor tableD = new HTableDescriptor(TableName.valueOf(tableName));
    HColumnDescriptor columnD = new HColumnDescriptor(Bytes.toBytes(familyName));
    tableD.addFamily(columnD);

    byte[][] splitKeys = new byte[10][1];
    splitKeys[0][0] = '0';
    splitKeys[1][0] = '1';
    splitKeys[2][0] = '2';
    splitKeys[3][0] = '3';
    splitKeys[4][0] = '4';
    splitKeys[5][0] = '5';
    splitKeys[6][0] = '6';
    splitKeys[7][0] = '7';
    splitKeys[8][0] = '8';
    splitKeys[9][0] = '9';

    admin.createTable(tableD, splitKeys);

    System.out.println("Getting HConnection");

    config.set("hbase.client.retries.number", "1");
    config.set("hbase.client.pause", "1");

    final HConnection connection = HConnectionManagerMultiClusterWrapper.createConnection(config);

    System.out.println(" - Got HConnection: " + connection.getClass());

    System.out.println("Getting HTable");

    final AtomicInteger threadFinishCounter = new AtomicInteger(0);

    for (int threadNum = 0; threadNum < numberOfThreads; threadNum++) {

        final BufferedWriter writer = new BufferedWriter(
                new FileWriter(outputCsvFile + "/thread-" + threadNum + ".csv"));

        final int threadFinalNum = threadNum;

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Random r = new Random();
                    for (int i = 1; i <= numberOfPuts; i++) {
                        HTableInterface table = connection.getTable(tableName);
                        HTableStats stats = ((HTableMultiCluster) table).getStats();
                        stats.printStats(writer, 5000);

                        int hash = r.nextInt(10);

                        Put put = new Put(Bytes.toBytes(
                                hash + ".key." + StringUtils.leftPad(String.valueOf(i * threadFinalNum), 12)));
                        put.add(Bytes.toBytes(familyName), Bytes.toBytes("C"),
                                Bytes.toBytes("Value:" + i * threadFinalNum));
                        table.put(put);

                        Thread.sleep(millisecondToWait);

                        Get get = new Get(Bytes.toBytes(
                                hash + ".key." + StringUtils.leftPad(String.valueOf(i * threadFinalNum), 12)));
                        table.get(get);

                        Thread.sleep(millisecondToWait);

                        Delete delete = new Delete(Bytes.toBytes(
                                hash + ".key." + StringUtils.leftPad(String.valueOf(i * threadFinalNum), 12)));
                        table.delete(delete);

                        Thread.sleep(millisecondToWait);

                        if (numberOfPuts % 10000 == 0) {
                            writeToSystemOut("{thread:" + threadFinalNum + ",count=" + i + "}", true);
                        } else if (numberOfPuts % 1000 == 0) {
                            writeToSystemOut(".", false);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    threadFinishCounter.incrementAndGet();
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t.start();
    }

    while (threadFinishCounter.get() < numberOfThreads) {
        Thread.sleep(millisecondToWait * 10);
    }

    admin.disableTable(TableName.valueOf(tableName));
    admin.deleteTable(TableName.valueOf(tableName));

    connection.close();
    admin.close();
}

From source file:org.apache.hadoop.hbase.test.MultiThreadedMultiClusterWithCmApiTest.java

public static void main(String[] args) throws Exception {
    if (args.length == 0) {

        System.out.println("RunMultiClusterTest " + "<CM-Host-1> " + "<UserName> " + "<Password> "
                + "<Cluster-1> " + "<HBase-Service-1> " + "<CM-Host-2> " + "<UserName-2> " + "<Password-2> "
                + "<Cluster-2> " + "<HBase-Service-2> " + "<tableName> " + "<familyName> " + "<numberOfPuts> "
                + "<millisecond of wait> " + "<numberOfThreads> " + "<outputCsvFile>");
    }//ww w  .jav a  2s . com

    final String cmHost1 = args[0];
    final String username1 = args[1];
    final String password1 = args[2];
    final String cluster1 = args[3];
    final String hbaseService1 = args[4];
    final String cmHost2 = args[5];
    final String username2 = args[6];
    final String password2 = args[7];
    final String cluster2 = args[8];
    final String hbaseService2 = args[9];

    LOG.info("--Getting Configurations");

    Configuration config = HBaseMultiClusterConfigUtil.combineConfigurations(cmHost1, username1, password1,
            cluster1, hbaseService1, cmHost2, username2, password2, cluster2, hbaseService2);

    LOG.info("--Got Configuration");

    final String tableName = args[10];
    final String familyName = args[11];
    final int numberOfPuts = Integer.parseInt(args[12]);
    final int millisecondToWait = Integer.parseInt(args[13]);
    final int numberOfThreads = Integer.parseInt(args[14]);
    final String outputCsvFile = args[15];

    LOG.info("Getting HAdmin");

    LOG.info(ConfigConst.HBASE_FAILOVER_CLUSTERS_CONFIG + ": "
            + config.get(ConfigConst.HBASE_FAILOVER_CLUSTERS_CONFIG));
    LOG.info("hbase.zookeeper.quorum: " + config.get("hbase.zookeeper.quorum"));
    LOG.info("hbase.failover.cluster.fail1.hbase.hstore.compaction.max: "
            + config.get("hbase.failover.cluster.fail1.hbase.hstore.compaction.max"));

    HBaseAdmin admin = new HBaseAdminMultiCluster(config);

    try {
        if (admin.tableExists(TableName.valueOf(tableName))) {
            try {
                admin.disableTable(TableName.valueOf(tableName));
            } catch (Exception e) {
                //nothing
            }
            admin.deleteTable(TableName.valueOf(tableName));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    LOG.info(" - Got HAdmin:" + admin.getClass());

    HTableDescriptor tableD = new HTableDescriptor(TableName.valueOf(tableName));
    HColumnDescriptor columnD = new HColumnDescriptor(Bytes.toBytes(familyName));
    tableD.addFamily(columnD);

    byte[][] splitKeys = new byte[10][1];
    splitKeys[0][0] = '0';
    splitKeys[1][0] = '1';
    splitKeys[2][0] = '2';
    splitKeys[3][0] = '3';
    splitKeys[4][0] = '4';
    splitKeys[5][0] = '5';
    splitKeys[6][0] = '6';
    splitKeys[7][0] = '7';
    splitKeys[8][0] = '8';
    splitKeys[9][0] = '9';

    LOG.info(" - About to create Table " + tableD.getName());

    admin.createTable(tableD, splitKeys);

    LOG.info(" - Created Table " + tableD.getName());

    LOG.info("Getting HConnection");

    config.set("hbase.client.retries.number", "1");
    config.set("hbase.client.pause", "1");

    final HConnection connection = HConnectionManagerMultiClusterWrapper.createConnection(config);

    LOG.info(" - Got HConnection: " + connection.getClass());

    LOG.info("Getting HTable");

    final AtomicInteger threadFinishCounter = new AtomicInteger(0);

    //Make sure output folder exist
    File outputFolder = new File(outputCsvFile);
    if (outputFolder.exists() == false) {
        outputFolder.mkdirs();
    }

    for (int threadNum = 0; threadNum < numberOfThreads; threadNum++) {

        final BufferedWriter writer = new BufferedWriter(
                new FileWriter(new File(outputCsvFile + "/thread-" + threadNum + ".csv")));

        final int threadFinalNum = threadNum;

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Random r = new Random();
                    HTableInterface table = connection.getTable(tableName);
                    HTableStats stats = ((HTableMultiCluster) table).getStats();
                    stats.printStats(writer, 5000);

                    for (int i = 1; i <= numberOfPuts; i++) {

                        int hash = r.nextInt(10);

                        Put put = new Put(Bytes.toBytes(hash + ".key." + i + "."
                                + StringUtils.leftPad(String.valueOf(i * threadFinalNum), 12)));
                        put.add(Bytes.toBytes(familyName), Bytes.toBytes("C"),
                                Bytes.toBytes("Value:" + i * threadFinalNum));
                        table.put(put);

                        Thread.sleep(millisecondToWait);

                        Get get = new Get(Bytes.toBytes(
                                hash + ".key." + StringUtils.leftPad(String.valueOf(i * threadFinalNum), 12)));
                        table.get(get);

                        Thread.sleep(millisecondToWait);

                        //Delete delete = new Delete(Bytes.toBytes(hash + ".key." + StringUtils.leftPad(String.valueOf(i * threadFinalNum), 12)));
                        //table.delete(delete);

                        Thread.sleep(millisecondToWait);

                        if (i % 10 == 0) {
                            writeToSystemOut("{thread:" + threadFinalNum + ",count=" + i + "}", true);
                        } else if (numberOfPuts % 1000 == 0) {
                            writeToSystemOut(".", false);
                        }
                    }
                    stats.stopPrintingStats();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    threadFinishCounter.incrementAndGet();
                    try {
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        t.start();
    }

    while (threadFinishCounter.get() < numberOfThreads) {
        Thread.sleep(millisecondToWait * 10);
    }

    //admin.disableTable(TableName.valueOf(tableName));
    //admin.deleteTable(TableName.valueOf(tableName));

    System.out.println("close connection");
    connection.close();
    System.out.println("close admin");
    admin.close();
    System.out.println("done");
    System.exit(0);
}

From source file:Main.java

/**
 * Generate a value suitable for use in {@ link #setId(int)}.
 * This value will not collide with ID values generated at build time by aapt for R.id.
 * http://stackoverflow.com/a/15442997/151957
 * @return a generated ID value/* ww  w. j a  v a 2 s  . c  o  m*/
 */
public static int generateViewId() {
    AtomicInteger sNextGeneratedId = new AtomicInteger(10001);

    for (;;) {
        final int result = sNextGeneratedId.get();
        // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
        int newValue = result + 1;
        if (newValue > 0x00FFFFFF)
            newValue = 1; // Roll over to 1, not 0.
        if (sNextGeneratedId.compareAndSet(result, newValue)) {
            return result;
        }
    }
}

From source file:AtomicUtils.java

public static boolean tryIncrementIfGreaterThan(final AtomicInteger capacity, final int least) {
    int capa;// w  w  w.  j a v  a  2s .c o  m
    do {
        capa = capacity.get();
        if (capa <= least) {
            return false;
        }
    } while (!capacity.compareAndSet(capa, capa + 1));
    return true;
}

From source file:AtomicUtils.java

public static boolean tryDecrementIfGreaterThan(final AtomicInteger capacity, final int least) {
    int capa;// w ww.  j a v  a 2  s.  c  o m
    do {
        capa = capacity.get();
        if (capa <= least) {
            return false;
        }
    } while (!capacity.compareAndSet(capa, capa - 1));
    return true;
}

From source file:jef.testbase.JefTester.java

private static String[] analyzeEntry(String str, AtomicInteger i) {
    int start = str.indexOf(' ', i.get());
    if (start < 0)
        return null;
    start++;//from w  ww  .ja v a 2 s  .  co  m
    int keyEnd = str.indexOf('=', start);
    String key = str.substring(start, keyEnd);
    // 
    int urlstart = keyEnd + 1;
    char quot = str.charAt(urlstart);
    if (quot == '\'' || quot == '"') {
        urlstart++;
    }
    // ?
    int urlend = StringUtils.indexOfAny(str, END_CHARS, urlstart);
    String value = str.substring(urlstart, urlend);
    i.set(urlend + 1);
    return new String[] { key, value };
}