Example usage for com.google.common.collect Multimap size

List of usage examples for com.google.common.collect Multimap size

Introduction

In this page you can find the example usage for com.google.common.collect Multimap size.

Prototype

int size();

Source Link

Document

Returns the number of key-value pairs in this multimap.

Usage

From source file:com.twitter.aurora.scheduler.thrift.SchedulerThriftInterface.java

@Override
public Response getJobs(@Nullable String maybeNullRole) {
    Optional<String> ownerRole = Optional.fromNullable(maybeNullRole);

    // Ensure we only return one JobConfiguration for each JobKey.
    Map<IJobKey, IJobConfiguration> jobs = Maps.newHashMap();

    // Query the task store, find immediate jobs, and synthesize a JobConfiguration for them.
    // This is necessary because the ImmediateJobManager doesn't store jobs directly and
    // ImmediateJobManager#getJobs always returns an empty Collection.
    Query.Builder scope = ownerRole.isPresent() ? Query.roleScoped(ownerRole.get()) : Query.unscoped();
    Multimap<IJobKey, IScheduledTask> tasks = Tasks
            .byJobKey(Storage.Util.weaklyConsistentFetchTasks(storage, scope.active()));

    jobs.putAll(Maps.transformEntries(tasks.asMap(),
            new Maps.EntryTransformer<IJobKey, Collection<IScheduledTask>, IJobConfiguration>() {
                @Override//from w  w  w .j a v a  2  s. co  m
                public IJobConfiguration transformEntry(IJobKey jobKey, Collection<IScheduledTask> tasks) {

                    // Pick an arbitrary task for each immediate job. The chosen task might not be the most
                    // recent if the job is in the middle of an update or some shards have been selectively
                    // created.
                    TaskConfig firstTask = tasks.iterator().next().getAssignedTask().getTask().newBuilder();
                    return IJobConfiguration.build(
                            new JobConfiguration().setKey(jobKey.newBuilder()).setOwner(firstTask.getOwner())
                                    .setTaskConfig(firstTask).setInstanceCount(tasks.size()));
                }
            }));

    // Get cron jobs directly from the manager. Do this after querying the task store so the real
    // template JobConfiguration for a cron job will overwrite the synthesized one that could have
    // been created above.
    Predicate<IJobConfiguration> configFilter = ownerRole.isPresent()
            ? Predicates.compose(Predicates.equalTo(ownerRole.get()), JobKeys.CONFIG_TO_ROLE)
            : Predicates.<IJobConfiguration>alwaysTrue();
    jobs.putAll(Maps.uniqueIndex(FluentIterable.from(cronJobManager.getJobs()).filter(configFilter),
            JobKeys.FROM_CONFIG));

    return new Response().setResponseCode(OK).setResult(Result
            .getJobsResult(new GetJobsResult().setConfigs(IJobConfiguration.toBuildersSet(jobs.values()))));
}

From source file:org.corpus_tools.peppermodules.paula.Salt2PAULAMapper.java

/**
 * Maps audio data as data source. When audio data are connected to tokens,
 * a span for each connection is created and annotated with an audio
 * annotation referencing the audio file. When no Token is connected to the
 * audio source, a span is created for all tokens and an audio annotation is
 * added to that span.//  www  .j  a v a2s  .com
 */
public void mapSMedialDS() {
    Multimap<SMedialDS, SToken> map = LinkedHashMultimap.create();
    if ((getDocument().getDocumentGraph().getMedialRelations() != null)
            && (getDocument().getDocumentGraph().getMedialRelations().size() > 0)) {
        /**
         * Create a markable file which addresses all tokens, which have
         * references to the SAudioDS
         */
        for (SMedialRelation rel : getDocument().getDocumentGraph().getMedialRelations()) {
            map.put(rel.getTarget(), rel.getSource());
        }
    } else {
        if ((getDocument().getDocumentGraph().getMedialDSs() != null)
                && (getDocument().getDocumentGraph().getMedialDSs().size() > 0))
            for (SMedialDS audioDS : getDocument().getDocumentGraph().getMedialDSs()) {
                map.putAll(audioDS, getDocument().getDocumentGraph().getTokens());
            }
    }
    if (map.size() > 0) {
        StringBuffer fileName = new StringBuffer();
        fileName.append(getResourceURI().toFileString());
        if (!fileName.toString().endsWith("/")) {
            fileName.append("/");
        }
        fileName.append(getDocument().getName());
        fileName.append(".");
        fileName.append(PAULA_TYPE.MARK.getFileInfix());
        fileName.append(".");
        fileName.append("audio");
        fileName.append(".");
        fileName.append(PepperModule.ENDING_XML);
        File audioMarkFile = new File(fileName.toString());

        PAULAPrinter printer = getPAULAPrinter(audioMarkFile);

        if (!printer.hasPreamble) {
            printer.printPreambel(PAULA_TYPE.MARK, "audio",
                    generateFileName(getDocument().getDocumentGraph().getTokens().get(0)));
        }

        for (SMedialDS audio : map.keySet()) {
            try {
                printer.xml.writeEmptyElement(TAG_MARK_MARK);
                if (audio.getPath().fragment() != null) {
                    printer.xml.writeAttribute(ATT_ID, checkId(audio.getPath().fragment()));
                } else {
                    printer.xml.writeAttribute(ATT_ID, audio.getId());
                }
                printer.xml.writeAttribute(ATT_HREF,
                        generateXPointer(new ArrayList<SToken>(map.get(audio)), printer.base));
            } catch (XMLStreamException e) {
                throw new PepperModuleException(Salt2PAULAMapper.this, "Cannot write in file '"
                        + audioMarkFile.getAbsolutePath() + "', because of a nested exception. ", e);
            }
        }

        /**
         * Create a feature file which addresses all tokens, which addresses
         * the audio markable file
         */
        // copy referenced files
        File audioFeatFile = new File(audioMarkFile.getAbsolutePath().replace("." + PepperModule.ENDING_XML,
                "_feat." + PepperModule.ENDING_XML));
        printer = getPAULAPrinter(audioFeatFile);
        printer.printPreambel(PAULA_TYPE.FEAT, KW_AUDIO, audioMarkFile);

        for (SMedialDS audio : getDocument().getDocumentGraph().getMedialDSs()) {
            /**
             * Copy audio file and
             */
            String target = audioMarkFile.getAbsoluteFile().getParent();
            if (!target.endsWith("/")) {
                target = target + "/";
            }
            target = target + audio.getMediaReference().lastSegment();
            File audioFile = new File(target);
            try {
                String source = audio.getMediaReference().toFileString();
                if (source == null) {
                    source = audio.getMediaReference().toString();
                }
                Files.copy(new File(source).toPath(), audioFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                throw new PepperModuleException(Salt2PAULAMapper.this,
                        "Cannot copy audio file '" + audio.getMediaReference() + "', to +'" + target + "'. ",
                        e);
            }
            /**
             * Create a feature file which addresses all tokens, which
             * addresses the audio markable file
             */
            try {
                printer.xml.writeEmptyElement(TAG_FEAT_FEAT);
                printer.xml.writeAttribute(ATT_HREF, "#" + audio.getPath().fragment());
                printer.xml.writeAttribute(ATT_FEAT_FEAT_VAL, audioFile.getName());
            } catch (XMLStreamException e) {
                throw new PepperModuleException(Salt2PAULAMapper.this, "Cannot write in file '"
                        + audioFeatFile.getAbsolutePath() + "', because of a nested exception. ", e);
            }

        }
    }
}

From source file:com.flexive.core.search.PropertyEntry.java

/**
 * Create a new instance based on the given (search) property.
 *
 * @param searchProperty the search property
 * @param storage        the storage instance
 * @param ignoreCase     whether case should be ignored for this column
 * @throws FxSqlSearchException if the entry could not be created
 *///from   www  . j  ava  2 s.com
public PropertyEntry(Property searchProperty, ContentStorage storage, boolean ignoreCase)
        throws FxSqlSearchException {
    this.type = Type.PROPERTY_REF;
    this.environment = CacheAdmin.getEnvironment();
    if (searchProperty.isAssignment()) {
        try {
            if (StringUtils.isNumeric(searchProperty.getPropertyName())) {
                //#<id>
                assignment = (FxPropertyAssignment) environment
                        .getAssignment(Long.valueOf(searchProperty.getPropertyName()));
            } else {
                //XPath
                assignment = (FxPropertyAssignment) environment.getAssignment(searchProperty.getPropertyName());
            }
        } catch (ClassCastException ce) {
            throw unknownAssignmentException(searchProperty, ce);
        } catch (FxRuntimeException e) {
            if (e.getConverted() instanceof FxNotFoundException) {
                throw unknownAssignmentException(searchProperty, e);
            } else {
                throw new FxSqlSearchException(LOG, e, "ex.sqlSearch.query.failedToResolveAssignment",
                        searchProperty.getPropertyName(), e.getMessage());
            }
        }
        this.property = assignment.getProperty();
    } else {
        this.property = environment.getProperty(searchProperty.getPropertyName());

        // check if all assignments of the property are in the same table
        final List<FxPropertyAssignment> assignments = environment.getPropertyAssignments(property.getId(),
                false);
        final Multimap<String, FxPropertyAssignment> storageCounts = HashMultimap.create();
        boolean hasFlatStorageAssignments = false;
        for (FxPropertyAssignment pa : assignments) {
            if (pa.isFlatStorageEntry()) {
                hasFlatStorageAssignments = true;
                final FxFlatStorageMapping mapping = pa.getFlatStorageMapping();
                // group assignments by table, column, and level
                storageCounts.put(mapping.getStorage() + "." + mapping.getColumn() + "." + mapping.getLevel(),
                        pa);
            } else {
                storageCounts.put(storage.getTableName(property), pa);
            }
        }

        if (storageCounts.size() > 1 || hasFlatStorageAssignments) {
            // more than one storage, or only flat storage assignments

            // find the table with most occurances
            final List<Multiset.Entry<String>> tables = newArrayList(storageCounts.keys().entrySet());
            Collections.sort(tables, new Comparator<Multiset.Entry<String>>() {
                @Override
                public int compare(Multiset.Entry<String> o1, Multiset.Entry<String> o2) {
                    return FxSharedUtils.compare(o2.getCount(), o1.getCount());
                }
            });
            final String key = tables.get(0).getElement();
            final FxPropertyAssignment pa = storageCounts.get(key).iterator().next();
            if (pa.isFlatStorageEntry()) {
                // use assignment search. All assignments share the same flat storage table,
                // column and level, thus the "normal" assignment search can be used.
                assignment = pa;
            } else {
                assignment = null; // use "real" property search in the CONTENT_DATA table
                if (hasFlatStorageAssignments && LOG.isWarnEnabled()) {
                    // only write warning to log for now
                    LOG.warn(new FxExceptionMessage("ex.sqlSearch.err.select.propertyWithFlat",
                            this.property.getName(),
                            Iterables.filter(assignments, new Predicate<FxPropertyAssignment>() {
                                @Override
                                public boolean apply(FxPropertyAssignment input) {
                                    return input.isFlatStorageEntry();
                                }
                            })).getLocalizedMessage(FxContext.get().getLanguage()));
                }
            }
        } else {
            assignment = null; // nothing to do, use normal property search
        }
    }

    if (assignment != null && assignment.isFlatStorageEntry()) {
        // flat storage assignment search
        this.tableName = assignment.getFlatStorageMapping().getStorage();
        this.tbl = PropertyResolver.Table.T_CONTENT_DATA_FLAT;
    } else {
        // content_data assignment or property search
        this.tableName = storage.getTableName(property);
        if (this.tableName.equalsIgnoreCase(DatabaseConst.TBL_CONTENT)) {
            this.tbl = PropertyResolver.Table.T_CONTENT;
        } else if (this.tableName.equalsIgnoreCase(DatabaseConst.TBL_CONTENT_DATA)) {
            this.tbl = PropertyResolver.Table.T_CONTENT_DATA;
        } else {
            throw new FxSqlSearchException(LOG, "ex.sqlSearch.err.unknownPropertyTable", searchProperty,
                    this.tableName);
        }
    }

    this.readColumns = getReadColumns(storage, property);

    if (assignment != null && assignment.isFlatStorageEntry()) {
        final String column = StorageManager.getStorageImpl()
                .escapeFlatStorageColumn(assignment.getFlatStorageMapping().getColumn());
        this.filterColumn = !ignoreCase
                || assignment.getOption(FxStructureOption.OPTION_IN_UPPERCASE).isValueTrue()
                || (this.property.getDataType() != FxDataType.String1024
                        && this.property.getDataType() != FxDataType.Text
                        && this.property.getDataType() != FxDataType.HTML) ? column
                                // calculate upper-case function for text queries
                                : "UPPER(" + column + ")";
        this.flatColumnIndex = FxFlatStorageManager.getInstance().getColumnDataIndex(assignment);
        if (this.flatColumnIndex == -1) {
            throw new FxSqlSearchException(LOG, "ex.sqlSearch.init.flatMappingIndex", searchProperty);
        }
    } else {
        String fcol = ignoreCase
                && !this.property.getOption(FxStructureOption.OPTION_IN_UPPERCASE).isValueTrue()
                        ? storage.getQueryUppercaseColumn(this.property)
                        : this.readColumns[0];
        if (fcol == null) {
            fcol = this.readColumns == null ? null : this.readColumns[0];
        }
        this.filterColumn = fcol;
        this.flatColumnIndex = -1;
    }

    if (this.filterColumn == null) {
        throw new FxSqlSearchException(LOG, "ex.sqlSearch.init.propertyDoesNotHaveColumnMapping",
                searchProperty.getPropertyName());
    }

    if (this.tbl == PropertyResolver.Table.T_CONTENT_DATA) {
        switch (this.property.getDataType()) {
        case Number:
        case SelectMany:
            this.dataColumn = "FBIGINT";
            break;
        default:
            this.dataColumn = "FINT";
            break;
        }
    } else {
        this.dataColumn = null;
    }

    this.multilanguage = this.property.isMultiLang();
    this.functions.addAll(searchProperty.getFunctions());
    if (this.functions.size() > 0) {
        // use outmost function result type
        this.overrideDataType = this.functions.get(0).getOverrideDataType();
    }
}

From source file:org.moe.designer.rendering.LayoutlibCallback.java

/**
 * Searches for cycles in the {@code <include>} tag graph of the layout files we've
 * been asked to provide parsers for//from  w  w  w  .ja va 2  s. com
 */
private boolean findCycles() {
    Map<File, String> fileToLayout = Maps.newHashMap();
    Map<String, File> layoutToFile = Maps.newHashMap();
    Multimap<String, String> includeMap = ArrayListMultimap.create();
    for (File file : myParserFiles) {
        String layoutName = LintUtils.getLayoutName(file);
        layoutToFile.put(layoutName, file);
        fileToLayout.put(file, layoutName);
        try {
            String xml = Files.toString(file, Charsets.UTF_8);
            Document document = XmlUtils.parseDocumentSilently(xml, true);
            if (document != null) {
                NodeList includeNodeList = document.getElementsByTagName(VIEW_INCLUDE);
                for (int i = 0, n = includeNodeList.getLength(); i < n; i++) {
                    Element include = (Element) includeNodeList.item(i);
                    String included = include.getAttribute(ATTR_LAYOUT);
                    if (included.startsWith(LAYOUT_RESOURCE_PREFIX)) {
                        String resource = included.substring(LAYOUT_RESOURCE_PREFIX.length());
                        includeMap.put(layoutName, resource);
                    }
                }
            }
        } catch (IOException e) {
            LOG.warn("Could not check file " + file + " for cyclic dependencies", e);
        }
    }

    // We now have a DAG over the include dependencies in the layouts
    // Do a DFS to detect cycles

    // Perform DFS on the include graph and look for a cycle; if we find one, produce
    // a chain of includes on the way back to show to the user
    if (includeMap.size() > 0) {
        for (String from : includeMap.keySet()) {
            Set<String> visiting = Sets.newHashSetWithExpectedSize(includeMap.size());
            List<String> chain = dfs(from, visiting, includeMap);
            if (chain != null) {
                if (myLogger != null) {
                    RenderProblem.Html problem = RenderProblem.create(WARNING);
                    HtmlBuilder builder = problem.getHtmlBuilder();
                    builder.add("Found cyclical <include> chain: ");
                    boolean first = true;
                    Collections.reverse(chain);
                    for (String layout : chain) {
                        if (first) {
                            first = false;
                        } else {
                            builder.add(" includes ");
                        }
                        File file = layoutToFile.get(layout);
                        if (file != null) {
                            try {
                                String url = SdkUtils.fileToUrlString(file);
                                builder.addLink(layout, url);
                            } catch (MalformedURLException e) {
                                builder.add(layout);
                            }
                        } else {
                            builder.add(layout);
                        }
                    }

                    myLogger.addMessage(problem);
                }
                return true;
            }
        }
    }

    return false;
}

From source file:accumulo.balancer.GroupBalancer.java

private void balanceExtraMultiple(Map<TServerInstance, TserverGroupInfo> tservers, int maxExtraGroups,
        Moves moves, Multimap<String, TserverGroupInfo> extraMultiple, boolean alwaysAdd) {

    ArrayList<Pair<String, TserverGroupInfo>> serversToRemove = new ArrayList<>();
    for (TserverGroupInfo destTgi : tservers.values()) {
        Map<String, Integer> extras = destTgi.getExtras();
        if (alwaysAdd || extras.size() < maxExtraGroups) {
            serversToRemove.clear();/* ww w.j a  v  a 2 s  .co m*/
            for (String group : extraMultiple.keySet()) {
                if (!extras.containsKey(group)) {
                    Collection<TserverGroupInfo> sources = extraMultiple.get(group);
                    Iterator<TserverGroupInfo> iter = sources.iterator();
                    TserverGroupInfo srcTgi = iter.next();

                    int num = srcTgi.getExtras().get(group);

                    moves.move(group, 1, srcTgi, destTgi);

                    if (num == 2) {
                        serversToRemove.add(new Pair<String, TserverGroupInfo>(group, srcTgi));
                    }

                    if (destTgi.getExtras().size() >= maxExtraGroups || moves.size() >= getMaxMigrations()) {
                        break;
                    }
                }
            }

            for (Pair<String, TserverGroupInfo> pair : serversToRemove) {
                extraMultiple.remove(pair.getFirst(), pair.getSecond());
            }

            if (extraMultiple.size() == 0 || moves.size() >= getMaxMigrations()) {
                break;
            }
        }
    }
}

From source file:com.yahoo.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl.java

/**
 * Assign owner for specified ServiceUnit from the given candidates, following the the principles: 1) Optimum
 * distribution: fill up one broker till its load reaches optimum level (defined by underload threshold) before pull
 * another idle broker in; 2) Even distribution: once all brokers' load are above optimum level, maintain all
 * brokers to have even load; 3) Set the underload threshold to small value (like 1) for pure even distribution, and
 * high value (like 80) for pure optimum distribution;
 *
 * Strategy to select broker: 1) The first choice is the least loaded broker which is underload but not idle; 2) The
 * second choice is idle broker (if there is any); 3) Othewise simply select the least loaded broker if it is NOT
 * overloaded; 4) If all brokers are overloaded, select the broker with maximum available capacity (considering
 * brokers could have different hardware configuration, this usually means to select the broker with more hardware
 * resource);//w ww.j a  va2  s  .  c  o  m
 *
 * Broker's load level: 1) Load ranking (triggered by LoadReport update) estimate the load level according to the
 * resourse usage and namespace bundles already loaded by each broker; 2) When leader broker decide the owner for a
 * new namespace bundle, it may take time for the real owner to actually load the bundle and refresh LoadReport,
 * leader broker will store the bundle in a list called preAllocatedBundles, and the quota of all
 * preAllocatedBundles in preAllocatedQuotas, and re-estimate the broker's load level by putting the
 * preAllocatedQuota into calculation; 3) Everything (preAllocatedBundles and preAllocatedQuotas) will get reset in
 * load ranking.
 */
private ResourceUnit findBrokerForPlacement(Multimap<Long, ResourceUnit> candidates,
        ServiceUnitId serviceUnit) {
    long underloadThreshold = this.getLoadBalancerBrokerUnderloadedThresholdPercentage();
    long overloadThreshold = this.getLoadBalancerBrokerOverloadedThresholdPercentage();
    ResourceQuota defaultQuota = pulsar.getLocalZkCacheService().getResourceQuotaCache().getDefaultQuota();

    double minLoadPercentage = 101.0;
    long maxAvailability = -1;
    ResourceUnit idleRU = null;
    ResourceUnit maxAvailableRU = null;
    ResourceUnit randomRU = null;

    ResourceUnit selectedRU = null;
    ResourceUnitRanking selectedRanking = null;
    String serviceUnitId = serviceUnit.toString();
    synchronized (resourceUnitRankings) {
        long randomBrokerIndex = (candidates.size() > 0) ? (this.brokerRotationCursor % candidates.size()) : 0;
        // find the least loaded & not-idle broker
        for (Map.Entry<Long, ResourceUnit> candidateOwner : candidates.entries()) {
            ResourceUnit candidate = candidateOwner.getValue();
            randomBrokerIndex--;

            // skip broker which is not ranked. this should never happen except in unit test
            if (!resourceUnitRankings.containsKey(candidate)) {
                continue;
            }

            // check if this ServiceUnit is already pre-allocated
            String resourceUnitId = candidate.getResourceId();
            ResourceUnitRanking ranking = resourceUnitRankings.get(candidate);
            if (ranking.isServiceUnitPreAllocated(serviceUnitId)) {
                return candidate;
            }

            // check if this ServiceUnit is already loaded
            if (ranking.isServiceUnitLoaded(serviceUnitId)) {
                ranking.removeLoadedServiceUnit(serviceUnitId, this.getResourceQuota(serviceUnitId));
            }

            // record a random broker
            if (randomBrokerIndex < 0 && randomRU == null) {
                randomRU = candidate;
            }

            // check the available capacity
            double loadPercentage = ranking.getEstimatedLoadPercentage();
            double availablePercentage = Math.max(0, (100 - loadPercentage) / 100);
            long availability = (long) (ranking.estimateMaxCapacity(defaultQuota) * availablePercentage);
            if (availability > maxAvailability) {
                maxAvailability = availability;
                maxAvailableRU = candidate;
            }

            // check the load percentage
            if (ranking.isIdle()) {
                if (idleRU == null) {
                    idleRU = candidate;
                }
            } else {
                if (selectedRU == null) {
                    selectedRU = candidate;
                    selectedRanking = ranking;
                    minLoadPercentage = loadPercentage;
                } else {
                    if (ranking.compareTo(selectedRanking) < 0) {
                        minLoadPercentage = loadPercentage;
                        selectedRU = candidate;
                        selectedRanking = ranking;
                    }
                }
            }
        }

        if ((minLoadPercentage > underloadThreshold && idleRU != null) || selectedRU == null) {
            // assigned to idle broker is the least loaded broker already have optimum load (which means NOT
            // underloaded), or all brokers are idle
            selectedRU = idleRU;
        } else if (minLoadPercentage >= 100.0 && randomRU != null) {
            // all brokers are full, assign to a random one
            selectedRU = randomRU;
        } else if (minLoadPercentage > overloadThreshold) {
            // assign to the broker with maximum available capacity if all brokers are overloaded
            selectedRU = maxAvailableRU;
        }

        // re-calculate load level for selected broker
        if (selectedRU != null) {
            this.brokerRotationCursor = (this.brokerRotationCursor + 1) % 1000000;
            ResourceUnitRanking ranking = resourceUnitRankings.get(selectedRU);
            String loadPercentageDesc = ranking.getEstimatedLoadPercentageString();
            log.info("Assign {} to {} with ({}).", serviceUnitId, selectedRU.getResourceId(),
                    loadPercentageDesc);
            if (!ranking.isServiceUnitPreAllocated(serviceUnitId)) {
                ResourceQuota quota = this.getResourceQuota(serviceUnitId);
                ranking.addPreAllocatedServiceUnit(serviceUnitId, quota);
            }
        }
    }
    return selectedRU;
}

From source file:com.github.pms1.tppt.p2.FeatureXmlComparator.java

private void comparePlugins2(Map<String, Multimap<String, Element>> baseline,
        Map<String, Multimap<String, Element>> current, ElementDeltaReporter elementDeltaReporter,
        DeltaReporter deltaReporter) {//from  w  w w  .  j  a v  a 2  s.c  o m

    for (String id : Sets.union(baseline.keySet(), current.keySet())) {
        Multimap<String, Element> b = baseline.get(id);
        if (b == null)
            b = HashMultimap.create();
        else
            b = HashMultimap.create(b);

        Multimap<String, Element> c = current.get(id);
        if (c == null)
            c = HashMultimap.create();
        else
            c = HashMultimap.create(c);

        AttributesDeltaReporter r = new AttributesDeltaReporter() {

            @Override
            public void removed(String key) {
                deltaReporter.fileDelta("Plugin {0} attribute {1} removed", id, key);
            }

            @Override
            public void changed(String key, String left, String right) {
                if (key.equals("version")) {
                    deltaReporter.pluginVersionDelta(id, left, right);
                } else {
                    deltaReporter.fileDelta("Plugin {0} attribute {1} changed {2} -> {3}", id, key, left,
                            right);
                }
            }

            @Override
            public void added(String key, String value) {
                deltaReporter.fileDelta("Plugin {0} attribute {1} / {2} added", id, key, value);
            }
        };

        Set<String> intersection = new HashSet<>(b.keys());
        intersection.retainAll(c.keys());

        for (String v : intersection) {
            Collection<Element> be = b.get(v);
            Collection<Element> ce = c.get(v);
            if (be.size() == 1 && ce.size() == 1) {
                compareAttributes(Iterables.getOnlyElement(be), Iterables.getOnlyElement(ce), r);
                b.removeAll(v);
                c.removeAll(v);
            }
        }

        if (b.size() == 1 && c.size() == 1) {
            compareAttributes(Iterables.getOnlyElement(b.values()), Iterables.getOnlyElement(c.values()), r);
        } else {
            for (Element e : b.values())
                deltaReporter.fileDelta("Plugin removed: {0}", domRenderer.render(e));
            for (Element e : c.values())
                deltaReporter.fileDelta("Plugin added: {0}", domRenderer.render(e));
        }
    }

}

From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraClientPool.java

private void sanityCheckRingConsistency() {
    Multimap<Set<TokenRange>, InetSocketAddress> tokenRangesToHost = HashMultimap.create();
    for (InetSocketAddress host : currentPools.keySet()) {
        Cassandra.Client client = null;//from  w w  w  .  jav  a  2s.c  o m
        try {
            client = CassandraClientFactory.getClientInternal(host, config.ssl(), config.socketTimeoutMillis(),
                    config.socketQueryTimeoutMillis());
            try {
                client.describe_keyspace(config.keyspace());
            } catch (NotFoundException e) {
                return; // don't care to check for ring consistency when we're not even fully initialized
            }
            tokenRangesToHost.put(ImmutableSet.copyOf(client.describe_ring(config.keyspace())), host);
        } catch (Exception e) {
            log.warn("failed to get ring info from host: {}", host, e);
        } finally {
            if (client != null) {
                client.getOutputProtocol().getTransport().close();
            }
        }

        if (tokenRangesToHost.isEmpty()) {
            log.warn(
                    "Failed to get ring info for entire Cassandra cluster ({}); ring could not be checked for consistency.",
                    config.keyspace());
            return;
        }

        if (tokenRangesToHost.keySet().size() == 1) { // all nodes agree on a consistent view of the cluster. Good.
            return;
        }

        RuntimeException e = new IllegalStateException(
                "Hosts have differing ring descriptions.  This can lead to inconsistent reads and lost data. ");
        log.error("QA-86204 " + e.getMessage() + tokenRangesToHost, e);

        // provide some easier to grok logging for the two most common cases
        if (tokenRangesToHost.size() > 2) {
            for (Map.Entry<Set<TokenRange>, Collection<InetSocketAddress>> entry : tokenRangesToHost.asMap()
                    .entrySet()) {
                if (entry.getValue().size() == 1) {
                    log.error("Host: " + entry.getValue().iterator().next()
                            + " disagrees with the other nodes about the ring state.");
                }
            }
        }
        if (tokenRangesToHost.keySet().size() == 2) {
            ImmutableList<Set<TokenRange>> sets = ImmutableList.copyOf(tokenRangesToHost.keySet());
            Set<TokenRange> set1 = sets.get(0);
            Set<TokenRange> set2 = sets.get(1);
            log.error("Hosts are split.  group1: " + tokenRangesToHost.get(set1) + " group2: "
                    + tokenRangesToHost.get(set2));
        }

        CassandraVerifier.logErrorOrThrow(e.getMessage(), config.safetyDisabled());
    }
}

From source file:com.yahoo.pulsar.broker.loadbalance.impl.SimpleLoadManagerImpl.java

private ResourceUnit getLeastLoadedBroker(ServiceUnitId serviceUnit,
        Map<Long, Set<ResourceUnit>> availableBrokers) {
    ResourceUnit selectedBroker = null;/*from w  w w.  jav  a2s .c  om*/
    Multimap<Long, ResourceUnit> finalCandidates = getFinalCandidates(serviceUnit, availableBrokers);
    // Remove candidates that point to inactive brokers
    Set<String> activeBrokers = Collections.emptySet();
    try {
        activeBrokers = availableActiveBrokers.get();
        // Need to use an explicit Iterator object to prevent concurrent modification exceptions
        Iterator<Map.Entry<Long, ResourceUnit>> candidateIterator = finalCandidates.entries().iterator();
        while (candidateIterator.hasNext()) {
            Map.Entry<Long, ResourceUnit> candidate = candidateIterator.next();
            String candidateBrokerName = candidate.getValue().getResourceId().replace("http://", "");
            if (!activeBrokers.contains(candidateBrokerName)) {
                candidateIterator.remove(); // Current candidate points to an inactive broker, so remove it
            }
        }
    } catch (Exception e) {
        log.warn("Error during attempt to remove inactive brokers while searching for least active broker", e);
    }

    if (finalCandidates.size() > 0) {
        if (this.getLoadBalancerPlacementStrategy().equals(LOADBALANCER_STRATEGY_LLS)) {
            selectedBroker = findBrokerForPlacement(finalCandidates, serviceUnit);
        } else {
            selectedBroker = placementStrategy.findBrokerForPlacement(finalCandidates);
        }
        log.debug("Selected : [{}] for ServiceUnit : [{}]", selectedBroker.getResourceId(),
                serviceUnit.getNamespaceObject().toString());
        return selectedBroker;
    } else {
        // No available broker found
        log.warn("No broker available to acquire service unit: [{}]", serviceUnit);
        return null;
    }
}

From source file:com.cloudant.sync.datastore.DatastoreImpl.java

/**
 * Removes revisions present in the datastore from the input map.
 *
 * @param revisions an multimap from document id to set of revisions. The
 *                  map is modified in place for performance consideration.
 *///from w ww  .  java  2 s  .co m
void revsDiffBatch(SQLDatabase db, Multimap<String, String> revisions) throws DatastoreException {

    final String sql = String.format(
            "SELECT docs.docid, revs.revid FROM docs, revs "
                    + "WHERE docs.doc_id = revs.doc_id AND docs.docid IN (%s) AND revs.revid IN (%s) "
                    + "ORDER BY docs.docid",
            DatabaseUtils.makePlaceholders(revisions.keySet().size()),
            DatabaseUtils.makePlaceholders(revisions.size()));

    String[] args = new String[revisions.keySet().size() + revisions.size()];
    String[] keys = revisions.keySet().toArray(new String[revisions.keySet().size()]);
    String[] values = revisions.values().toArray(new String[revisions.size()]);
    System.arraycopy(keys, 0, args, 0, revisions.keySet().size());
    System.arraycopy(values, 0, args, revisions.keySet().size(), revisions.size());

    Cursor cursor = null;
    try {
        cursor = db.rawQuery(sql, args);
        while (cursor.moveToNext()) {
            String docId = cursor.getString(0);
            String revId = cursor.getString(1);
            revisions.remove(docId, revId);
        }
    } catch (SQLException e) {
        throw new DatastoreException(e);
    } finally {
        DatabaseUtils.closeCursorQuietly(cursor);
    }
}