Example usage for java.util.concurrent ConcurrentLinkedQueue peek

List of usage examples for java.util.concurrent ConcurrentLinkedQueue peek

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentLinkedQueue peek.

Prototype

public E peek() 

Source Link

Usage

From source file:com.ibm.crail.storage.StorageServer.java

public static void main(String[] args) throws Exception {
    Logger LOG = CrailUtils.getLogger();
    CrailConfiguration conf = new CrailConfiguration();
    CrailConstants.updateConstants(conf);
    CrailConstants.printConf();/*from   www  .jav  a 2 s  .c  o m*/
    CrailConstants.verify();

    int splitIndex = 0;
    for (String param : args) {
        if (param.equalsIgnoreCase("--")) {
            break;
        }
        splitIndex++;
    }

    //default values
    StringTokenizer tokenizer = new StringTokenizer(CrailConstants.STORAGE_TYPES, ",");
    if (!tokenizer.hasMoreTokens()) {
        throw new Exception("No storage types defined!");
    }
    String storageName = tokenizer.nextToken();
    int storageType = 0;
    HashMap<String, Integer> storageTypes = new HashMap<String, Integer>();
    storageTypes.put(storageName, storageType);
    for (int type = 1; tokenizer.hasMoreElements(); type++) {
        String name = tokenizer.nextToken();
        storageTypes.put(name, type);
    }
    int storageClass = -1;

    //custom values
    if (args != null) {
        Option typeOption = Option.builder("t").desc("storage type to start").hasArg().build();
        Option classOption = Option.builder("c").desc("storage class the server will attach to").hasArg()
                .build();
        Options options = new Options();
        options.addOption(typeOption);
        options.addOption(classOption);
        CommandLineParser parser = new DefaultParser();

        try {
            CommandLine line = parser.parse(options, Arrays.copyOfRange(args, 0, splitIndex));
            if (line.hasOption(typeOption.getOpt())) {
                storageName = line.getOptionValue(typeOption.getOpt());
                storageType = storageTypes.get(storageName).intValue();
            }
            if (line.hasOption(classOption.getOpt())) {
                storageClass = Integer.parseInt(line.getOptionValue(classOption.getOpt()));
            }
        } catch (ParseException e) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("Storage tier", options);
            System.exit(-1);
        }
    }
    if (storageClass < 0) {
        storageClass = storageType;
    }

    StorageTier storageTier = StorageTier.createInstance(storageName);
    if (storageTier == null) {
        throw new Exception("Cannot instantiate datanode of type " + storageName);
    }

    String extraParams[] = null;
    splitIndex++;
    if (args.length > splitIndex) {
        extraParams = new String[args.length - splitIndex];
        for (int i = splitIndex; i < args.length; i++) {
            extraParams[i - splitIndex] = args[i];
        }
    }
    storageTier.init(conf, extraParams);
    storageTier.printConf(LOG);

    RpcClient rpcClient = RpcClient.createInstance(CrailConstants.NAMENODE_RPC_TYPE);
    rpcClient.init(conf, args);
    rpcClient.printConf(LOG);

    ConcurrentLinkedQueue<InetSocketAddress> namenodeList = CrailUtils.getNameNodeList();
    ConcurrentLinkedQueue<RpcConnection> connectionList = new ConcurrentLinkedQueue<RpcConnection>();
    while (!namenodeList.isEmpty()) {
        InetSocketAddress address = namenodeList.poll();
        RpcConnection connection = rpcClient.connect(address);
        connectionList.add(connection);
    }
    RpcConnection rpcConnection = connectionList.peek();
    if (connectionList.size() > 1) {
        rpcConnection = new RpcDispatcher(connectionList);
    }
    LOG.info("connected to namenode(s) " + rpcConnection.toString());

    StorageServer server = storageTier.launchServer();
    StorageRpcClient storageRpc = new StorageRpcClient(storageType, CrailStorageClass.get(storageClass),
            server.getAddress(), rpcConnection);

    HashMap<Long, Long> blockCount = new HashMap<Long, Long>();
    long sumCount = 0;
    while (server.isAlive()) {
        StorageResource resource = server.allocateResource();
        if (resource == null) {
            break;
        } else {
            storageRpc.setBlock(resource.getAddress(), resource.getLength(), resource.getKey());
            DataNodeStatistics stats = storageRpc.getDataNode();
            long newCount = stats.getFreeBlockCount();
            long serviceId = stats.getServiceId();

            long oldCount = 0;
            if (blockCount.containsKey(serviceId)) {
                oldCount = blockCount.get(serviceId);
            }
            long diffCount = newCount - oldCount;
            blockCount.put(serviceId, newCount);
            sumCount += diffCount;
            LOG.info("datanode statistics, freeBlocks " + sumCount);
        }
    }

    while (server.isAlive()) {
        DataNodeStatistics stats = storageRpc.getDataNode();
        long newCount = stats.getFreeBlockCount();
        long serviceId = stats.getServiceId();

        long oldCount = 0;
        if (blockCount.containsKey(serviceId)) {
            oldCount = blockCount.get(serviceId);
        }
        long diffCount = newCount - oldCount;
        blockCount.put(serviceId, newCount);
        sumCount += diffCount;

        LOG.info("datanode statistics, freeBlocks " + sumCount);
        Thread.sleep(2000);
    }
}

From source file:com.gistlabs.mechanize.cache.HttpCacheFilterTest.java

@Test
public void confirmConcurrentLinkedQueueOfferSemantics() {
    ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();

    assertTrue(queue.offer("a"));
    assertEquals(1, queue.size());//from  w  ww . ja  v a2s.  c o  m

    assertTrue(queue.offer("b"));
    assertEquals(2, queue.size());
    assertEquals("a", queue.peek());

    assertTrue(queue.offer("a"));
    //assertEquals(2, queue.size());
    //assertEquals("b", queue.peek());
    assertEquals(3, queue.size());
    assertEquals("a", queue.peek());
}

From source file:com.chinamobile.bcbsp.comm.MessageQueuesForDisk.java

@Override
public IMessage getAMessage() {
    ConcurrentLinkedQueue<IMessage> incomingQueue = null;
    // Get the hash bucket index.
    int hashIndex = (int) (Math.random() * 10);
    BucketMeta meta = this.incomingQueues.get(hashIndex);
    // The bucket is on disk.
    if (meta.onDiskFlag) {
        this.incomingFileLocks[hashIndex].lock();
        /* Lock */
        try {// w  w w  .  ja v a2s.co  m
            loadBucket(this.incomingQueues, hashIndex, "incoming");
        } catch (IOException e) {
            LOG.error("[MessageQueuesForDisk:removeIncomingQueue]", e);
        } finally {
            this.incomingFileLocks[hashIndex].unlock();
            /** Unlock */
        }
    }
    meta = this.incomingQueues.get(hashIndex);
    // incomingQueue = meta.queueMap.remove(dstVertexID);
    Iterator<String> it = meta.queueMap.keySet().iterator();
    IMessage mes = null;
    while (it.hasNext()) {
        String vertexID = it.next();
        if (vertexID != null) {
            ConcurrentLinkedQueue<IMessage> messages = meta.queueMap.get(vertexID);
            if (messages == null) {
                continue;
            }
            mes = messages.peek();
        }
        break;
    }
    return mes;
}

From source file:org.dlw.ai.blackboard.Blackboard.java

/**
 * Public method allowing expert (knowledge source) a chance at the
 * blackboard model to evaluate the problem domain
 * //from  w  w w.j av a  2 s .c  o  m
 * @param ks
 *            the {@link org.dlw.ai.blackboard.knowledge.KnowledgeSource} at
 *            the board (presently)
 */
public final void connect(KnowledgeSource ks) {

    ConcurrentLinkedQueue<Assumption> queue = ks.getPastAssumptions();

    if (queue.size() > 0) {

        Assumption assumption = queue.peek();

        /**
         * Search the ArrayList for our one and only sentence
         */
        Sentence sentence = getSentence();

        /**
         * make affirmation statement on letter-stack (push assumptions)
         */
        updateAffirmations(sentence, assumption);
    }

    //BlackboardUtil.outputSnapshot(this);
    BlackboardUtil.outputProgress(this);

}

From source file:org.wso2.developerstudio.eclipse.greg.manager.remote.views.RegistryBrowserView.java

private String searchRegistryNodeForResource(RegistryNode node, String caption)
        throws InvalidRegistryURLException, UnknownRegistryException {
    ConcurrentLinkedQueue<RegistryResourceNode> queue = new ConcurrentLinkedQueue<RegistryResourceNode>();
    queue.addAll(node.getRegistryContainer().getRegistryContent());
    while (queue.peek() != null) {
        RegistryResourceNode registryResourceNode = queue.poll();
        if (caption.equalsIgnoreCase(registryResourceNode.getCaption())) {
            return registryResourceNode.getRegistryResourcePath();
        } else {//from   ww w. ja va2s . c om
            queue.addAll(registryResourceNode.getResourceNodeList());
        }
    }

    //      for (RegistryResourceNode registryResourceNode : queue) {
    //         if(caption.equalsIgnoreCase(registryResourceNode.getCaption())){
    //              return registryResourceNode.getRegistryResourcePath();
    //           }else{
    //              queue.addAll(registryResourceNode.getResourceNodeList());
    //           }   
    //        }
    return null;
}

From source file:org.wso2.developerstudio.eclipse.greg.manager.remote.views.RegistryBrowserView.java

private RegistryResourceNode searchRegistryNodeForResourceNode(RegistryNode node, String caption)
        throws InvalidRegistryURLException, UnknownRegistryException {
    ConcurrentLinkedQueue<RegistryResourceNode> queue = new ConcurrentLinkedQueue<RegistryResourceNode>();
    queue.addAll(node.getRegistryContainer().getRegistryContent());
    while (queue.peek() != null) {
        RegistryResourceNode registryResourceNode = queue.poll();
        if (caption.equalsIgnoreCase(registryResourceNode.getCaption())) {
            return registryResourceNode;
        } else {/* w  ww.  j a  v  a2  s  .c  om*/
            queue.addAll(registryResourceNode.getResourceNodeList());
        }
    }

    //      for (RegistryResourceNode registryResourceNode : queue) {
    //         if(caption.equalsIgnoreCase(registryResourceNode.getCaption())){
    //              return registryResourceNode.getRegistryResourcePath();
    //           }else{
    //              queue.addAll(registryResourceNode.getResourceNodeList());
    //           }   
    //        }
    return null;
}