Search in sources :

Example 1 with SolrjNamedThreadFactory

use of org.apache.solr.common.util.SolrjNamedThreadFactory in project lucene-solr by apache.

the class FeaturesSelectionStream method open.

/**
   * Opens the CloudSolrStream
   *
   ***/
public void open() throws IOException {
    if (cache == null) {
        isCloseCache = true;
        cache = new SolrClientCache();
    } else {
        isCloseCache = false;
    }
    this.cloudSolrClient = this.cache.getCloudSolrClient(zkHost);
    this.executorService = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrjNamedThreadFactory("FeaturesSelectionStream"));
}
Also used : SolrjNamedThreadFactory(org.apache.solr.common.util.SolrjNamedThreadFactory) SolrClientCache(org.apache.solr.client.solrj.io.SolrClientCache)

Example 2 with SolrjNamedThreadFactory

use of org.apache.solr.common.util.SolrjNamedThreadFactory in project lucene-solr by apache.

the class ShortestPathStream method open.

public void open() throws IOException {
    List<Map<String, List<String>>> allVisited = new ArrayList();
    Map visited = new HashMap();
    visited.put(this.fromNode, null);
    allVisited.add(visited);
    int depth = 0;
    Map<String, List<String>> nextVisited = null;
    List<Edge> targets = new ArrayList();
    ExecutorService threadPool = null;
    try {
        threadPool = ExecutorUtil.newMDCAwareFixedThreadPool(threads, new SolrjNamedThreadFactory("ShortestPathStream"));
        //Breadth first search
        TRAVERSE: while (targets.size() == 0 && depth < maxDepth) {
            Set<String> nodes = visited.keySet();
            Iterator<String> it = nodes.iterator();
            nextVisited = new HashMap();
            int batchCount = 0;
            List<String> queryNodes = new ArrayList();
            List<Future> futures = new ArrayList();
            JOIN: //Queue up all the batches
            while (it.hasNext()) {
                String node = it.next();
                queryNodes.add(node);
                ++batchCount;
                if (batchCount == joinBatchSize || !it.hasNext()) {
                    try {
                        JoinRunner joinRunner = new JoinRunner(queryNodes);
                        Future<List<Edge>> future = threadPool.submit(joinRunner);
                        futures.add(future);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    batchCount = 0;
                    queryNodes = new ArrayList();
                }
            }
            try {
                //Process the batches as they become available
                OUTER: for (Future<List<Edge>> future : futures) {
                    List<Edge> edges = future.get();
                    INNER: for (Edge edge : edges) {
                        if (toNode.equals(edge.to)) {
                            targets.add(edge);
                            if (nextVisited.containsKey(edge.to)) {
                                List<String> parents = nextVisited.get(edge.to);
                                parents.add(edge.from);
                            } else {
                                List<String> parents = new ArrayList();
                                parents.add(edge.from);
                                nextVisited.put(edge.to, parents);
                            }
                        } else {
                            if (!cycle(edge.to, allVisited)) {
                                if (nextVisited.containsKey(edge.to)) {
                                    List<String> parents = nextVisited.get(edge.to);
                                    parents.add(edge.from);
                                } else {
                                    List<String> parents = new ArrayList();
                                    parents.add(edge.from);
                                    nextVisited.put(edge.to, parents);
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            allVisited.add(nextVisited);
            visited = nextVisited;
            ++depth;
        }
    } finally {
        threadPool.shutdown();
    }
    Set<String> finalPaths = new HashSet();
    if (targets.size() > 0) {
        for (Edge edge : targets) {
            List<LinkedList> paths = new ArrayList();
            LinkedList<String> path = new LinkedList();
            path.addFirst(edge.to);
            paths.add(path);
            //Walk back up the tree a collect the parent nodes.
            INNER: for (int i = allVisited.size() - 1; i >= 0; --i) {
                Map<String, List<String>> v = allVisited.get(i);
                Iterator<LinkedList> it = paths.iterator();
                List newPaths = new ArrayList();
                while (it.hasNext()) {
                    LinkedList p = it.next();
                    List<String> parents = v.get(p.peekFirst());
                    if (parents != null) {
                        for (String parent : parents) {
                            LinkedList newPath = new LinkedList();
                            newPath.addAll(p);
                            newPath.addFirst(parent);
                            newPaths.add(newPath);
                        }
                        paths = newPaths;
                    }
                }
            }
            for (LinkedList p : paths) {
                String s = p.toString();
                if (!finalPaths.contains(s)) {
                    Tuple shortestPath = new Tuple(new HashMap());
                    shortestPath.put("path", p);
                    shortestPaths.add(shortestPath);
                    finalPaths.add(s);
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) SolrjNamedThreadFactory(org.apache.solr.common.util.SolrjNamedThreadFactory) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Tuple(org.apache.solr.client.solrj.io.Tuple) HashSet(java.util.HashSet)

Example 3 with SolrjNamedThreadFactory

use of org.apache.solr.common.util.SolrjNamedThreadFactory in project lucene-solr by apache.

the class TopicStream method openStreams.

private void openStreams() throws IOException {
    ExecutorService service = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrjNamedThreadFactory("TopicStream"));
    try {
        List<Future<TupleWrapper>> futures = new ArrayList();
        for (TupleStream solrStream : solrStreams) {
            StreamOpener so = new StreamOpener((SolrStream) solrStream, comp);
            Future<TupleWrapper> future = service.submit(so);
            futures.add(future);
        }
        try {
            for (Future<TupleWrapper> f : futures) {
                TupleWrapper w = f.get();
                if (w != null) {
                    tuples.add(w);
                }
            }
        } catch (Exception e) {
            throw new IOException(e);
        }
    } finally {
        service.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) SolrjNamedThreadFactory(org.apache.solr.common.util.SolrjNamedThreadFactory) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with SolrjNamedThreadFactory

use of org.apache.solr.common.util.SolrjNamedThreadFactory in project lucene-solr by apache.

the class HttpSolrClient method httpUriRequest.

/**
   * @lucene.experimental
   */
public HttpUriRequestResponse httpUriRequest(final SolrRequest request, final ResponseParser processor) throws SolrServerException, IOException {
    HttpUriRequestResponse mrr = new HttpUriRequestResponse();
    final HttpRequestBase method = createMethod(request, null);
    ExecutorService pool = ExecutorUtil.newMDCAwareFixedThreadPool(1, new SolrjNamedThreadFactory("httpUriRequest"));
    try {
        MDC.put("HttpSolrClient.url", baseUrl);
        mrr.future = pool.submit(() -> executeMethod(method, processor));
    } finally {
        pool.shutdown();
        MDC.remove("HttpSolrClient.url");
    }
    assert method != null;
    mrr.httpUriRequest = method;
    return mrr;
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) ExecutorService(java.util.concurrent.ExecutorService) SolrjNamedThreadFactory(org.apache.solr.common.util.SolrjNamedThreadFactory)

Example 5 with SolrjNamedThreadFactory

use of org.apache.solr.common.util.SolrjNamedThreadFactory in project lucene-solr by apache.

the class ConcurrentUpdateSolrClientTest method testConcurrentCollectionUpdate.

@Test
public void testConcurrentCollectionUpdate() throws Exception {
    int cussThreadCount = 2;
    int cussQueueSize = 100;
    int numDocs = 100;
    int numRunnables = 5;
    int expected = numDocs * numRunnables;
    try (ConcurrentUpdateSolrClient concurrentClient = (new ConcurrentUpdateSolrClient.Builder(jetty.getBaseUrl().toString())).withQueueSize(cussQueueSize).withThreadCount(cussThreadCount).build()) {
        concurrentClient.setPollQueueTime(0);
        // ensure it doesn't block where there's nothing to do yet
        concurrentClient.blockUntilFinished();
        // Delete all existing documents.
        concurrentClient.deleteByQuery("collection1", "*:*");
        int poolSize = 5;
        ExecutorService threadPool = ExecutorUtil.newMDCAwareFixedThreadPool(poolSize, new SolrjNamedThreadFactory("testCUSS"));
        for (int r = 0; r < numRunnables; r++) threadPool.execute(new SendDocsRunnable(String.valueOf(r), numDocs, concurrentClient, "collection1"));
        // ensure all docs are sent
        threadPool.awaitTermination(5, TimeUnit.SECONDS);
        threadPool.shutdown();
        concurrentClient.commit("collection1");
        assertEquals(expected, concurrentClient.query("collection1", new SolrQuery("*:*")).getResults().getNumFound());
        // wait until all requests are processed by CUSS 
        concurrentClient.blockUntilFinished();
        concurrentClient.shutdownNow();
    }
    try (ConcurrentUpdateSolrClient concurrentClient = (new ConcurrentUpdateSolrClient.Builder(jetty.getBaseUrl().toString() + "/collection1")).withQueueSize(cussQueueSize).withThreadCount(cussThreadCount).build()) {
        assertEquals(expected, concurrentClient.query(new SolrQuery("*:*")).getResults().getNumFound());
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) SolrjNamedThreadFactory(org.apache.solr.common.util.SolrjNamedThreadFactory) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Aggregations

SolrjNamedThreadFactory (org.apache.solr.common.util.SolrjNamedThreadFactory)12 ExecutorService (java.util.concurrent.ExecutorService)8 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Future (java.util.concurrent.Future)3 SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 Tuple (org.apache.solr.client.solrj.io.Tuple)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)1