Example usage for org.apache.solr.core CoreContainer getZkController

List of usage examples for org.apache.solr.core CoreContainer getZkController

Introduction

In this page you can find the example usage for org.apache.solr.core CoreContainer getZkController.

Prototype

public ZkController getZkController() 

Source Link

Usage

From source file:alba.components.FilteredShowFileRequestHandler.java

License:Apache License

private void showFromZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp, CoreContainer coreContainer)
        throws KeeperException, InterruptedException, UnsupportedEncodingException {

    SolrZkClient zkClient = coreContainer.getZkController().getZkClient();

    String adminFile = getAdminFileFromZooKeeper(req, rsp, zkClient, hiddenFiles);

    if (adminFile == null) {
        return;//from w  ww.  j  a  v a  2 s  . com
    }

    // Show a directory listing
    List<String> children = zkClient.getChildren(adminFile, null, true);
    if (children.size() > 0) {

        NamedList<SimpleOrderedMap<Object>> files = new SimpleOrderedMap<>();
        for (String f : children) {
            if (isHiddenFile(req, rsp, f, false, hiddenFiles)) {
                continue;
            }

            SimpleOrderedMap<Object> fileInfo = new SimpleOrderedMap<>();
            files.add(f, fileInfo);
            List<String> fchildren = zkClient.getChildren(adminFile + "/" + f, null, true);
            if (fchildren.size() > 0) {
                fileInfo.add("directory", true);
            } else {
                // TODO? content type
                fileInfo.add("size", f.length());
            }
            // TODO: ?
            // fileInfo.add( "modified", new Date( f.lastModified() ) );
        }
        rsp.add("files", files);
    } else {
        // Include the file contents
        // The file logic depends on RawResponseWriter, so force its use.
        ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
        params.set(CommonParams.WT, "raw");
        req.setParams(params);

        ContentStreamBase content = new ContentStreamBase.ByteArrayStream(
                zkClient.getData(adminFile, null, null, true), adminFile);
        content.setContentType(req.getParams().get(USE_CONTENT_TYPE));

        // Velocity parsing here!

        // http://velocity.apache.org/engine/devel/developer-guide.html#The_Context
        Velocity.init();

        VelocityContext context = new VelocityContext();

        //add some vars??
        //context.put( "context", new String("Velocity") );

        for (int i = 0; i < rsp.getValues().size(); i++) {
            context.put(rsp.getValues().getName(i), rsp.getValues().getVal(i));
        }

        Template template = null;

        String fname = req.getParams().get("file", null);

        try {
            //TODO what if fname is null?
            template = Velocity.getTemplate(fname);
        } catch (ResourceNotFoundException rnfe) {
            // couldn't find the template, try to load it

            // TODO it should be fired only for SOME mimetypes (..through an annotation??)
            StringBuilder sb = this.getTemplate(content);

            RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
            StringReader reader = new StringReader(sb.toString());
            SimpleNode node = null;
            try {
                node = runtimeServices.parse(reader, fname);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                logger.error("error while parsing new template", e);
            }

            template = new Template();

            template.setRuntimeServices(runtimeServices);

            if (node != null) {
                template.setData(node);
            } else {
                logger.error("node null, can't set on template");
            }

            template.initDocument();

        } catch (ParseErrorException pee) {
            // syntax error: problem parsing the template
            logger.error("error while parsing template: ", pee);

        } catch (MethodInvocationException mie) {
            // something invoked in the template
            // threw an exception
            logger.error("error while parsing temaplate: ", mie);
        } catch (Exception e) {
            logger.error("error while parsing temaplate: ", e);
        }

        StringWriter sw = new StringWriter();

        template.merge(context, sw);

        // http://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte
        content = new ContentStreamBase.ByteArrayStream(
                sw.getBuffer().toString().getBytes(Charset.forName("UTF-8")), adminFile);
        content.setContentType(req.getParams().get(USE_CONTENT_TYPE));

        rsp.add(RawResponseWriter.CONTENT, content);
    }
    rsp.setHttpCaching(false);
}

From source file:edu.harvard.gis.hhypermap.bop.solrplugins.DateShardRoutingSearchHandler.java

License:Apache License

private void addShardsParamIfWeCan(SolrQueryRequest req) {
    CoreDescriptor coreDescriptor = req.getCore().getCoreDescriptor();
    CoreContainer coreContainer = coreDescriptor.getCoreContainer();
    if (!coreContainer.isZooKeeperAware()) {
        return;/*from w  w  w .  j  a v  a2  s . c  o m*/
    }
    if (!req.getParams().getBool("distrib", true)) {
        return;
    }
    final String shards = req.getParams().get(ShardParams.SHARDS);
    if (shards != null) {
        return; // we already have the shards
    }

    String startStrParam = req.getParams().get(START_PARAM);
    String endStrParam = req.getParams().get(END_PARAM);

    Instant startInst = // null means open-ended ('*')
            startStrParam == null ? null : DateMathParser.parseMath(null, startStrParam).toInstant();
    Instant endInst = endStrParam == null ? null : DateMathParser.parseMath(null, endStrParam).toInstant();

    if (startInst == null && endInst == null) {
        return;
    }

    ZkController zkController = coreContainer.getZkController();
    String collection = req.getParams().get("collection", coreDescriptor.getCollectionName());
    List<Slice> slices = getOrderedSlices(zkController, collection);
    if (slices.size() <= 1) {
        return;
    }

    List<String> routeShardNames = new ArrayList<>(); // the result
    boolean findingStart = (startInst != null);
    String prevShardName = null;
    Instant prevShardStartKey = null;

    for (Slice slice : slices) {
        String name = slice.getName();
        Instant shardStartKey = parseStartKeyFromShardName(name);
        if (prevShardStartKey != null && prevShardStartKey.isAfter(shardStartKey)) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "shards not in order? " + slice);
        }
        if (findingStart) {
            // As we advance shards, is this one finally > the 'start'? If so, accept it.
            if (shardStartKey.isAfter(startInst)) {
                if (prevShardName != null) {
                    routeShardNames.add(prevShardName);
                }
                findingStart = false; // thus findingEnd
            }
        }
        if (!findingStart && endInst == null) { // take all the remainder since 'end' is null
            routeShardNames.add(name);
        } else if (!findingStart) { // findingEnd
            if (shardStartKey.isAfter(endInst)) {
                break;
            }
            routeShardNames.add(name);
        }

        prevShardName = name;
        prevShardStartKey = shardStartKey;
    }
    if (findingStart) {
        routeShardNames.add(prevShardName);
    }

    if (routeShardNames.size() == slices.size()) {
        return;
    }

    ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
    String shardsValue = StrUtils.join(routeShardNames, ',');
    params.set(ShardParams.SHARDS, shardsValue);
    req.setParams(params);
    log.debug("Set shards: {}", shardsValue);
}

From source file:org.vootoo.server.Vootoo.java

License:Apache License

public static Map<String, Integer> checkStateIsValid(CoreContainer cores, String stateVer) {
    Map<String, Integer> result = null;
    String[] pairs = null;//w  w  w.j  a  va 2s . co m
    if (stateVer != null && !stateVer.isEmpty() && cores.isZooKeeperAware()) {
        // many have multiple collections separated by |
        pairs = StringUtils.split(stateVer, '|');
        for (String pair : pairs) {
            String[] pcs = StringUtils.split(pair, ':');
            if (pcs.length == 2 && !pcs[0].isEmpty() && !pcs[1].isEmpty()) {
                Integer status = cores.getZkController().getZkStateReader().compareStateVersions(pcs[0],
                        Integer.parseInt(pcs[1]));
                if (status != null) {
                    if (result == null)
                        result = new HashMap<>();
                    result.put(pcs[0], status);
                }
            }
        }
    }
    return result;
}

From source file:org.vootoo.server.Vootoo.java

License:Apache License

public static SolrCore getCoreByCollection(CoreContainer cores, String corename) {
    ZkStateReader zkStateReader = cores.getZkController().getZkStateReader();

    ClusterState clusterState = zkStateReader.getClusterState();
    Map<String, Slice> slices = clusterState.getActiveSlicesMap(corename);
    if (slices == null) {
        return null;
    }//ww  w .j  ava2s.  com
    // look for a core on this node
    Set<Map.Entry<String, Slice>> entries = slices.entrySet();
    SolrCore core = null;
    done: for (Map.Entry<String, Slice> entry : entries) {
        // first see if we have the leader
        ZkNodeProps leaderProps = clusterState.getLeader(corename, entry.getKey());
        if (leaderProps != null) {
            core = checkProps(cores, leaderProps);
        }
        if (core != null) {
            break done;
        }

        // check everyone then
        Map<String, Replica> shards = entry.getValue().getReplicasMap();
        Set<Map.Entry<String, Replica>> shardEntries = shards.entrySet();
        for (Map.Entry<String, Replica> shardEntry : shardEntries) {
            Replica zkProps = shardEntry.getValue();
            core = checkProps(cores, zkProps);
            if (core != null) {
                break done;
            }
        }
    }
    return core;
}

From source file:org.vootoo.server.Vootoo.java

License:Apache License

public static SolrCore checkProps(CoreContainer cores, ZkNodeProps zkProps) {
    String corename;/*from w w  w  .  ja  v  a2 s  .c  o m*/
    SolrCore core = null;
    if (cores.getZkController().getNodeName().equals(zkProps.getStr(ZkStateReader.NODE_NAME_PROP))) {
        corename = zkProps.getStr(ZkStateReader.CORE_NAME_PROP);
        core = cores.getCore(corename);
    }
    return core;
}