Search in sources :

Example 11 with Collections2

use of com.google.common.collect.Collections2 in project copybara by google.

the class GitHubPrOrigin method checkRequiredStatusContextNames.

/**
 * Check that the PR has a state of "success" for each status whose context is in the list
 * provided in the `required_status_context_names` param
 */
private void checkRequiredStatusContextNames(GitHubApi api, String project, PullRequest prData) throws ValidationException, RepoException {
    Set<String> requiredStatusContextNames = getRequiredStatusContextNames();
    if (forceImport() || requiredStatusContextNames.isEmpty()) {
        return;
    }
    try (ProfilerTask ignore = generalOptions.profiler().start("github_api_get_combined_status")) {
        CombinedStatus combinedStatus = api.getCombinedStatus(project, prData.getHead().getSha());
        Set<String> requiredButNotPresent = Sets.newHashSet(requiredStatusContextNames);
        List<Status> successStatuses = combinedStatus.getStatuses().stream().filter(e -> e.getState() == State.SUCCESS).collect(Collectors.toList());
        requiredButNotPresent.removeAll(Collections2.transform(successStatuses, Status::getContext));
        if (!requiredButNotPresent.isEmpty()) {
            throw new EmptyChangeException(String.format("Cannot migrate http://github.com/%s/pull/%d because the following ci labels " + "have not been passed: %s", project, prData.getNumber(), requiredButNotPresent));
        }
    }
}
Also used : CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Status(com.google.copybara.git.github.api.Status) GitHubUtil.asHeadRef(com.google.copybara.git.github.util.GitHubUtil.asHeadRef) Origin(com.google.copybara.Origin) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus) Collections2(com.google.common.collect.Collections2) Review(com.google.copybara.git.github.api.Review) ImmutableListMultimap.toImmutableListMultimap(com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap) Matcher(java.util.regex.Matcher) Change(com.google.copybara.Change) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Endpoint(com.google.copybara.Endpoint) Splitter(com.google.common.base.Splitter) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) User(com.google.copybara.git.github.api.User) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) List(java.util.List) GitHubApi(com.google.copybara.git.github.api.GitHubApi) PullRequest(com.google.copybara.git.github.api.PullRequest) GitHubUtil.asMergeRef(com.google.copybara.git.github.util.GitHubUtil.asMergeRef) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) AutoValue(com.google.auto.value.AutoValue) Optional(java.util.Optional) Joiner(com.google.common.base.Joiner) AuthorAssociation(com.google.copybara.git.github.api.AuthorAssociation) Iterables(com.google.common.collect.Iterables) CheckRuns(com.google.copybara.git.github.api.CheckRuns) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) SubmoduleStrategy(com.google.copybara.git.GitOrigin.SubmoduleStrategy) HashSet(java.util.HashSet) GitHubUtil(com.google.copybara.git.github.util.GitHubUtil) Label(com.google.copybara.git.github.api.Label) State(com.google.copybara.git.github.api.Status.State) ImmutableList(com.google.common.collect.ImmutableList) Issue(com.google.copybara.git.github.api.Issue) Nullable(javax.annotation.Nullable) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CharMatcher(com.google.common.base.CharMatcher) ValidationException(com.google.copybara.exception.ValidationException) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) PatchTransformation(com.google.copybara.transform.patch.PatchTransformation) Console(com.google.copybara.util.console.Console) TimeUnit(java.util.concurrent.TimeUnit) Authoring(com.google.copybara.authoring.Authoring) Checker(com.google.copybara.checks.Checker) Glob(com.google.copybara.util.Glob) CheckRun(com.google.copybara.git.github.api.CheckRun) Preconditions(com.google.common.base.Preconditions) Status(com.google.copybara.git.github.api.Status) GitHubHost(com.google.copybara.git.github.util.GitHubHost) GitHubPrUrl(com.google.copybara.git.github.util.GitHubHost.GitHubPrUrl) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) CombinedStatus(com.google.copybara.git.github.api.CombinedStatus)

Example 12 with Collections2

use of com.google.common.collect.Collections2 in project controller by opendaylight.

the class JMXGeneratorTest method generateMBEsTest.

@Test
public void generateMBEsTest() throws Exception {
    // default value for module factory file is true
    map.put(JMXGenerator.MODULE_FACTORY_FILE_BOOLEAN, "randomValue");
    jmxGenerator.setAdditionalConfig(map);
    Collection<File> files = jmxGenerator.generateSources(context, outputBaseDir, Collections.singleton(threadsJavaModule), m -> Optional.empty());
    assertEquals(expectedModuleFileNames, toFileNames(files));
    for (File file : files) {
        final String name = file.getName();
        if (!name.endsWith("java")) {
            continue;
        }
        MbeASTVisitor visitor = new MbeASTVisitor(EXPECTED_PACKAGE_PREFIX + ".threads.java", name);
        verifyFile(file, visitor);
        switch(name) {
            case "AbstractDynamicThreadPoolModule.java":
                assertAbstractDynamicThreadPoolModule(visitor);
                break;
            case "AsyncEventBusModuleMXBean.java":
                assertEquals("Incorrenct number of generated methods", 4, visitor.methods.size());
                break;
            case "AbstractNamingThreadFactoryModuleFactory.java":
                assertAbstractNamingThreadFactoryModuleFactory(visitor);
                break;
            case "AsyncEventBusModule.java":
                assertContains(visitor.extnds, EXPECTED_PACKAGE_PREFIX + ".threads.java.AbstractAsyncEventBusModule");
                visitor.assertFields(0);
                assertEquals("Incorrenct number of generated methods", 2, visitor.methods.size());
                visitor.assertConstructors(2);
                visitor.assertMethodDescriptions(0);
                visitor.assertMethodJavadocs(0);
                break;
            case "EventBusModuleFactory.java":
                assertContains(visitor.extnds, EXPECTED_PACKAGE_PREFIX + ".threads.java.AbstractEventBusModuleFactory");
                visitor.assertFields(0);
                assertEquals("Incorrenct number of generated methods", 0, visitor.methods.size());
                visitor.assertConstructors(0);
                visitor.assertMethodDescriptions(0);
                visitor.assertMethodJavadocs(0);
                break;
        }
    }
    verifyXmlFiles(Collections2.filter(files, input -> input.getName().endsWith("xml")));
    // verify ModuleFactory file
    File moduleFactoryFile = JMXGenerator.concatFolders(generatedResourcesDir, "META-INF", "services", ModuleFactory.class.getName());
    assertTrue(moduleFactoryFile.exists());
    Set<String> lines = ImmutableSet.copyOf(Files.readLines(moduleFactoryFile, StandardCharsets.UTF_8));
    Set<String> expectedLines = ImmutableSet.of(EXPECTED_PACKAGE_PREFIX + ".threads.java.EventBusModuleFactory", EXPECTED_PACKAGE_PREFIX + ".threads.java.AsyncEventBusModuleFactory", EXPECTED_PACKAGE_PREFIX + ".threads.java.DynamicThreadPoolModuleFactory", EXPECTED_PACKAGE_PREFIX + ".threads.java.NamingThreadFactoryModuleFactory", EXPECTED_PACKAGE_PREFIX + ".threads.java.ThreadPoolRegistryImplModuleFactory");
    assertEquals(expectedLines, lines);
}
Also used : ConfigConstants(org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants) DynamicMBeanWithInstance(org.opendaylight.controller.config.api.DynamicMBeanWithInstance) HashMap(java.util.HashMap) ModuleFactory(org.opendaylight.controller.config.spi.ModuleFactory) Collections2(com.google.common.collect.Collections2) AbstractModule(org.opendaylight.controller.config.spi.AbstractModule) DependencyResolverFactory(org.opendaylight.controller.config.api.DependencyResolverFactory) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) EXPECTED_PACKAGE_PREFIX(org.opendaylight.controller.config.yangjmxgenerator.PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX) ErrorHandler(org.xml.sax.ErrorHandler) ParseException(com.github.javaparser.ParseException) Files(com.google.common.io.Files) MavenProject(org.apache.maven.project.MavenProject) Assert.fail(org.junit.Assert.fail) ServiceInterfaceEntryTest(org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntryTest) CompilationUnit(com.github.javaparser.ast.CompilationUnit) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) InputSource(org.xml.sax.InputSource) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ImmutableSet(com.google.common.collect.ImmutableSet) Module(org.opendaylight.controller.config.spi.Module) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) BundleContext(org.osgi.framework.BundleContext) List(java.util.List) SAXParseException(org.xml.sax.SAXParseException) DependencyResolver(org.opendaylight.controller.config.api.DependencyResolver) Assert.assertFalse(org.junit.Assert.assertFalse) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) JavaParser(com.github.javaparser.JavaParser) ModuleFactory(org.opendaylight.controller.config.spi.ModuleFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) ServiceInterfaceEntryTest(org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntryTest) Test(org.junit.Test)

Example 13 with Collections2

use of com.google.common.collect.Collections2 in project atlasdb by palantir.

the class SweepStatsKeyValueService method flushWrites.

private void flushWrites(Multiset<TableReference> writes, Set<TableReference> clears) {
    if (writes.isEmpty() && clears.isEmpty()) {
        log.info("No writes to flush");
        return;
    }
    log.info("Flushing stats for {} writes and {} clears", SafeArg.of("writes", writes.size()), SafeArg.of("clears", clears.size()));
    log.trace("Flushing writes: {}", UnsafeArg.of("writes", writes));
    log.trace("Flushing clears: {}", UnsafeArg.of("clears", clears));
    try {
        Set<TableReference> tableNames = Sets.difference(writes.elementSet(), clears);
        Collection<byte[]> rows = Collections2.transform(Collections2.transform(tableNames, t -> t.getQualifiedName()), Functions.compose(Persistables.persistToBytesFunction(), SweepPriorityRow.fromFullTableNameFun()));
        Map<Cell, Value> oldWriteCounts = delegate().getRows(SWEEP_PRIORITY_TABLE, rows, SweepPriorityTable.getColumnSelection(SweepPriorityNamedColumn.WRITE_COUNT), Long.MAX_VALUE);
        Map<Cell, byte[]> newWriteCounts = Maps.newHashMapWithExpectedSize(writes.elementSet().size());
        byte[] col = SweepPriorityNamedColumn.WRITE_COUNT.getShortName();
        for (TableReference tableRef : tableNames) {
            Preconditions.checkState(!tableRef.getQualifiedName().startsWith(AtlasDbConstants.NAMESPACE_PREFIX), "The sweep stats kvs should wrap the namespace mapping kvs, not the other way around.");
            byte[] row = SweepPriorityRow.of(tableRef.getQualifiedName()).persistToBytes();
            Cell cell = Cell.create(row, col);
            Value oldValue = oldWriteCounts.get(cell);
            long oldCount = oldValue == null || oldValue.getContents().length == 0 ? 0 : SweepPriorityTable.WriteCount.BYTES_HYDRATOR.hydrateFromBytes(oldValue.getContents()).getValue();
            long newValue = clears.contains(tableRef) ? writes.count(tableRef) : oldCount + writes.count(tableRef);
            log.debug("Sweep priority for {} has {} writes (was {})", tableRef, newValue, oldCount);
            newWriteCounts.put(cell, SweepPriorityTable.WriteCount.of(newValue).persistValue());
        }
        long timestamp = timestampService.getFreshTimestamp();
        // Committing before writing is intentional, we want the start timestamp to
        // show up in the transaction table before we write do our writes.
        commit(timestamp);
        delegate().put(SWEEP_PRIORITY_TABLE, newWriteCounts, timestamp);
    } catch (RuntimeException e) {
        if (Thread.interrupted()) {
            return;
        }
        Set<TableReference> allTableNames = delegate().getAllTableNames();
        if (!allTableNames.contains(SWEEP_PRIORITY_TABLE) || !allTableNames.contains(TransactionConstants.TRANSACTION_TABLE)) {
            // ignore problems when sweep or transaction tables don't exist
            log.warn("Ignoring failed sweep stats flush due to ", e);
        }
        log.error("Unable to flush sweep stats for writes {} and clears {}: ", writes, clears, e);
        throw e;
    }
}
Also used : Multiset(com.google.common.collect.Multiset) LoggerFactory(org.slf4j.LoggerFactory) SweepSchema(com.palantir.atlasdb.schema.SweepSchema) SweepPriorityRow(com.palantir.atlasdb.schema.generated.SweepPriorityTable.SweepPriorityRow) Collections2(com.google.common.collect.Collections2) Multimap(com.google.common.collect.Multimap) Supplier(java.util.function.Supplier) Persistables(com.palantir.common.persist.Persistables) SweepPriorityNamedColumn(com.palantir.atlasdb.schema.generated.SweepPriorityTable.SweepPriorityNamedColumn) ClusterAvailabilityStatus(com.palantir.atlasdb.keyvalue.api.ClusterAvailabilityStatus) SafeArg(com.palantir.logsafe.SafeArg) PTExecutors(com.palantir.common.concurrent.PTExecutors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) TimestampService(com.palantir.timestamp.TimestampService) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) Functions(com.google.common.base.Functions) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Value(com.palantir.atlasdb.keyvalue.api.Value) ImmutableMap(com.google.common.collect.ImmutableMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) SweepPriorityTable(com.palantir.atlasdb.schema.generated.SweepPriorityTable) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) TransactionConstants(com.palantir.atlasdb.transaction.impl.TransactionConstants) ConcurrentHashMultiset(com.google.common.collect.ConcurrentHashMultiset) UnsafeArg(com.palantir.logsafe.UnsafeArg) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) Entry(java.util.Map.Entry) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Value(com.palantir.atlasdb.keyvalue.api.Value) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Aggregations

Collections2 (com.google.common.collect.Collections2)13 Set (java.util.Set)11 List (java.util.List)10 Collection (java.util.Collection)9 Collections (java.util.Collections)7 ImmutableSet (com.google.common.collect.ImmutableSet)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Map (java.util.Map)6 Preconditions (com.google.common.base.Preconditions)4 Iterables (com.google.common.collect.Iterables)4 Sets (com.google.common.collect.Sets)4 File (java.io.File)4 HashMap (java.util.HashMap)4 Optional (java.util.Optional)4 TimeUnit (java.util.concurrent.TimeUnit)4 AutoValue (com.google.auto.value.AutoValue)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 Splitter (com.google.common.base.Splitter)3