Search in sources :

Example 46 with Collections

use of java.util.Collections in project pravega by pravega.

the class ThrottlerTests method testUninterruptibleDelay.

/**
 * Tests the case when an interruption is called on an uninterruptible throttling source.
 *
 * @throws Exception
 */
@Test
public void testUninterruptibleDelay() throws Exception {
    // Supply monotonically decreasing delays.
    val suppliedDelays = Arrays.asList(2000);
    val delays = Collections.<Integer>synchronizedList(new ArrayList<>());
    val calculator = new TestCalculatorThrottler(ThrottlerCalculator.ThrottlerName.Batching);
    val nextDelay = suppliedDelays.iterator();
    Consumer<Integer> recordDelay = delayMillis -> {
        delays.add(delayMillis);
        // 0 means we're done (no more throttling).
        calculator.setDelayMillis(nextDelay.hasNext() ? nextDelay.next() : 0);
    };
    @Cleanup TestThrottler t = new TestThrottler(this.containerId, wrap(calculator), executorService(), metrics, recordDelay);
    // Set a non-maximum delay and ask to throttle, then verify we throttled the correct amount.
    calculator.setDelayMillis(nextDelay.next());
    val t1 = t.throttle();
    Assert.assertFalse("Not expected throttle future to be completed yet.", t1.isDone());
    t.notifyThrottleSourceChanged();
    TestUtils.await(t1::isDone, 5, TIMEOUT_MILLIS);
    Assert.assertEquals("Last reported delay is equal to last supplied delay.", (int) suppliedDelays.get(0), (int) getThrottlerMetric(calculator.getName()));
}
Also used : lombok.val(lombok.val) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) Setter(lombok.Setter) Getter(lombok.Getter) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cleanup(lombok.Cleanup) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MetricsNames(io.pravega.shared.MetricsNames) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ConfigurationException(io.pravega.common.util.ConfigurationException) Before(org.junit.Before) MetricsTags(io.pravega.shared.MetricsTags) SegmentStoreMetrics(io.pravega.segmentstore.server.SegmentStoreMetrics) SerializedClassRunner(io.pravega.test.common.SerializedClassRunner) lombok.val(lombok.val) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MetricsProvider(io.pravega.shared.metrics.MetricsProvider) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) MetricRegistryUtils(io.pravega.shared.metrics.MetricRegistryUtils) MetricsConfig(io.pravega.shared.metrics.MetricsConfig) TestUtils(io.pravega.test.common.TestUtils) Assert(org.junit.Assert) Collections(java.util.Collections) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 47 with Collections

use of java.util.Collections in project pravega by pravega.

the class ThrottlerTests method testInterruptedDelay.

/**
 * Tests the case when {@link Throttler#throttle()} returns a delay that can be interrupted using {@link Throttler#notifyThrottleSourceChanged()}}.
 */
private void testInterruptedDelay(ThrottlerCalculator.ThrottlerName throttlerName) throws Exception {
    val suppliedDelays = Arrays.asList(5000, 2500, 5000);
    val delays = Collections.<Integer>synchronizedList(new ArrayList<>());
    val calculator = new TestCalculatorThrottler(throttlerName);
    val nextDelay = suppliedDelays.iterator();
    Consumer<Integer> recordDelay = delayMillis -> {
        delays.add(delayMillis);
        // 0 means we're done (no more throttling).
        calculator.setDelayMillis(nextDelay.hasNext() ? nextDelay.next() : 0);
    };
    @Cleanup TestThrottler t = new TestThrottler(this.containerId, wrap(calculator), executorService(), metrics, recordDelay);
    // Set a non-maximum delay and ask to throttle, then verify we throttled the correct amount.
    calculator.setDelayMillis(nextDelay.next());
    val t1 = t.throttle();
    Assert.assertFalse("Not expected throttle future to be completed yet.", t1.isDone());
    // currently running throttle cycle and request the next throttling value.
    for (int i = 1; i < suppliedDelays.size(); i++) {
        // Interrupt the current throttle cycle.
        t.notifyThrottleSourceChanged();
        Assert.assertFalse("Not expected throttle future to be completed yet.", t1.isDone());
        // Wait for the new cycle to begin (we use the recordDelay consumer above to figure this out).
        int expectedDelayCount = i + 1;
        TestUtils.await(() -> delays.size() == expectedDelayCount, 5, TIMEOUT_MILLIS);
    }
    // When we are done, complete the last throttle cycle and check final results.
    t.completeDelayFuture();
    TestUtils.await(t1::isDone, 5, TIMEOUT_MILLIS);
    Assert.assertEquals("Unexpected number of delays recorded.", suppliedDelays.size(), delays.size());
    Assert.assertEquals("Unexpected first delay value.", suppliedDelays.get(0), delays.get(0));
    // runs and different environments. We can only check that they decrease in value (by design).
    for (int i = 1; i < delays.size(); i++) {
        AssertExtensions.assertLessThanOrEqual("Expected delays to be decreasing.", delays.get(i - 1), delays.get(i));
    }
}
Also used : lombok.val(lombok.val) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) Setter(lombok.Setter) Getter(lombok.Getter) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) RunWith(org.junit.runner.RunWith) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Cleanup(lombok.Cleanup) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) MetricsNames(io.pravega.shared.MetricsNames) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ConfigurationException(io.pravega.common.util.ConfigurationException) Before(org.junit.Before) MetricsTags(io.pravega.shared.MetricsTags) SegmentStoreMetrics(io.pravega.segmentstore.server.SegmentStoreMetrics) SerializedClassRunner(io.pravega.test.common.SerializedClassRunner) lombok.val(lombok.val) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) MetricsProvider(io.pravega.shared.metrics.MetricsProvider) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) MetricRegistryUtils(io.pravega.shared.metrics.MetricRegistryUtils) MetricsConfig(io.pravega.shared.metrics.MetricsConfig) TestUtils(io.pravega.test.common.TestUtils) Assert(org.junit.Assert) Collections(java.util.Collections) Cleanup(lombok.Cleanup)

Example 48 with Collections

use of java.util.Collections in project alf.io by alfio-event.

the class EventApiV1Controller method insertEvent.

private Optional<Event> insertEvent(EventCreationRequest request, Principal user, String imageRef) {
    Organization organization = userManager.findUserOrganizations(user.getName()).get(0);
    EventModification em = request.toEventModification(organization, eventNameManager::generateShortName, imageRef);
    eventManager.createEvent(em, user.getName());
    Optional<Event> event = eventManager.getOptionalByName(em.getShortName(), user.getName());
    event.ifPresent(e -> {
        Optional.ofNullable(request.getTickets().getPromoCodes()).ifPresent(promoCodes -> promoCodes.forEach(// TODO add ref to categories
        pc -> eventManager.addPromoCode(pc.getName(), e.getId(), organization.getId(), ZonedDateTime.of(pc.getValidFrom(), e.getZoneId()), ZonedDateTime.of(pc.getValidTo(), e.getZoneId()), pc.getDiscount(), pc.getDiscountType(), Collections.emptyList(), null, null, null, PromoCodeDiscount.CodeType.DISCOUNT, null)));
        // link categories to groups, if any
        request.getTickets().getCategories().stream().filter(cr -> cr.getGroupLink() != null && cr.getGroupLink().getGroupId() != null).map(cr -> Pair.of(cr, groupManager.findById(cr.getGroupLink().getGroupId(), organization.getId()))).forEach(link -> {
            if (link.getRight().isPresent()) {
                Group group = link.getRight().get();
                EventCreationRequest.CategoryRequest categoryRequest = link.getLeft();
                findCategoryByName(e, categoryRequest.getName()).ifPresent(category -> {
                    EventCreationRequest.GroupLinkRequest groupLinkRequest = categoryRequest.getGroupLink();
                    LinkedGroupModification modification = new LinkedGroupModification(null, group.getId(), e.getId(), category.getId(), groupLinkRequest.getType(), groupLinkRequest.getMatchType(), groupLinkRequest.getMaxAllocation());
                    groupManager.createLink(group.getId(), e.getId(), modification);
                });
            }
        });
        if (!CollectionUtils.isEmpty(request.getExtensionSettings())) {
            request.getExtensionSettings().stream().collect(Collectors.groupingBy(EventCreationRequest.ExtensionSetting::getExtensionId)).forEach((id, settings) -> {
                List<ExtensionSupport.ExtensionMetadataIdAndName> metadata = extensionService.getSingle(organization, e, id).map(es -> extensionRepository.findAllParametersForExtension(es.getId())).orElseGet(Collections::emptyList);
                List<ExtensionMetadataValue> values = settings.stream().map(es -> Pair.of(es, metadata.stream().filter(mm -> mm.getName().equals(es.getKey())).findFirst())).filter(pair -> {
                    if (pair.getRight().isEmpty()) {
                        log.warn("ignoring non-existent extension setting key {}", pair.getLeft().getKey());
                    }
                    return pair.getRight().isPresent();
                }).map(pair -> new ExtensionMetadataValue(pair.getRight().get().getId(), pair.getLeft().getValue())).collect(Collectors.toList());
                extensionService.bulkUpdateEventSettings(organization, e, values);
            });
        }
    });
    return event;
}
Also used : ValidationResult(alfio.model.result.ValidationResult) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) alfio.manager(alfio.manager) Group(alfio.model.group.Group) Errors(org.springframework.validation.Errors) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) ZonedDateTime(java.time.ZonedDateTime) ConfigurationManager(alfio.manager.system.ConfigurationManager) ExtensionRepository(alfio.repository.ExtensionRepository) EventModification(alfio.model.modification.EventModification) AtomicReference(java.util.concurrent.atomic.AtomicReference) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) CollectionUtils(org.apache.commons.collections.CollectionUtils) ExtensionMetadataValue(alfio.model.ExtensionSupport.ExtensionMetadataValue) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Organization(alfio.model.user.Organization) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) Collectors(java.util.stream.Collectors) LinkedGroupModification(alfio.model.modification.LinkedGroupModification) Result(alfio.model.result.Result) List(java.util.List) ExtensionService(alfio.extension.ExtensionService) Principal(java.security.Principal) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) alfio.model(alfio.model) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Group(alfio.model.group.Group) Organization(alfio.model.user.Organization) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) LinkedGroupModification(alfio.model.modification.LinkedGroupModification) EventModification(alfio.model.modification.EventModification) ExtensionMetadataValue(alfio.model.ExtensionSupport.ExtensionMetadataValue) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) Collections(java.util.Collections)

Example 49 with Collections

use of java.util.Collections in project elasticsearch by elastic.

the class TermsQueryBuilder method convert.

/**
     * Convert the list in a way that optimizes storage in the case that all
     * elements are either integers or {@link String}s/{@link BytesRef}s. This
     * is useful to help garbage collections for use-cases that involve sending
     * very large terms queries to Elasticsearch. If the list does not only
     * contain integers or {@link String}s, then a list is returned where all
     * {@link String}s have been replaced with {@link BytesRef}s.
     */
static List<?> convert(List<?> list) {
    if (list.isEmpty()) {
        return Collections.emptyList();
    }
    final boolean allNumbers = list.stream().allMatch(o -> o != null && INTEGER_TYPES.contains(o.getClass()));
    if (allNumbers) {
        final long[] elements = list.stream().mapToLong(o -> ((Number) o).longValue()).toArray();
        return new AbstractList<Object>() {

            @Override
            public Object get(int index) {
                return elements[index];
            }

            @Override
            public int size() {
                return elements.length;
            }
        };
    }
    final boolean allStrings = list.stream().allMatch(o -> o != null && STRING_TYPES.contains(o.getClass()));
    if (allStrings) {
        final BytesRefBuilder builder = new BytesRefBuilder();
        try (BytesStreamOutput bytesOut = new BytesStreamOutput()) {
            final int[] endOffsets = new int[list.size()];
            int i = 0;
            for (Object o : list) {
                BytesRef b;
                if (o instanceof BytesRef) {
                    b = (BytesRef) o;
                } else {
                    builder.copyChars(o.toString());
                    b = builder.get();
                }
                bytesOut.writeBytes(b.bytes, b.offset, b.length);
                if (i == 0) {
                    endOffsets[0] = b.length;
                } else {
                    endOffsets[i] = Math.addExact(endOffsets[i - 1], b.length);
                }
                ++i;
            }
            final BytesReference bytes = bytesOut.bytes();
            return new AbstractList<Object>() {

                public Object get(int i) {
                    final int startOffset = i == 0 ? 0 : endOffsets[i - 1];
                    final int endOffset = endOffsets[i];
                    return bytes.slice(startOffset, endOffset - startOffset).toBytesRef();
                }

                public int size() {
                    return endOffsets.length;
                }
            };
        }
    }
    return list.stream().map(o -> o instanceof String ? new BytesRef(o.toString()) : o).collect(Collectors.toList());
}
Also used : Query(org.apache.lucene.search.Query) IntStream(java.util.stream.IntStream) GetResponse(org.elasticsearch.action.get.GetResponse) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Arrays(java.util.Arrays) ParsingException(org.elasticsearch.common.ParsingException) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) AbstractList(java.util.AbstractList) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) XContentMapValues(org.elasticsearch.common.xcontent.support.XContentMapValues) GetRequest(org.elasticsearch.action.get.GetRequest) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) BytesRef(org.apache.lucene.util.BytesRef) Client(org.elasticsearch.client.Client) Set(java.util.Set) TermsLookup(org.elasticsearch.indices.TermsLookup) IOException(java.io.IOException) BytesReference(org.elasticsearch.common.bytes.BytesReference) Collectors(java.util.stream.Collectors) XContentParser(org.elasticsearch.common.xcontent.XContentParser) Objects(java.util.Objects) List(java.util.List) StreamInput(org.elasticsearch.common.io.stream.StreamInput) BytesRefs(org.elasticsearch.common.lucene.BytesRefs) Queries(org.elasticsearch.common.lucene.search.Queries) Collections(java.util.Collections) AbstractList(java.util.AbstractList) BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesRef(org.apache.lucene.util.BytesRef)

Example 50 with Collections

use of java.util.Collections in project torodb by torodb.

the class RecoveryService method cloneDatabases.

private void cloneDatabases(@Nonnull MongoClient remoteClient) throws CloningException, MongoException, UserException {
    enableDataImportMode();
    try {
        Stream<String> dbNames;
        try (MongoConnection remoteConnection = remoteClient.openConnection()) {
            RemoteCommandResponse<ListDatabasesReply> remoteResponse = remoteConnection.execute(ListDatabasesCommand.INSTANCE, "admin", true, Empty.getInstance());
            if (!remoteResponse.isOk()) {
                throw remoteResponse.asMongoException();
            }
            dbNames = remoteResponse.getCommandReply().get().getDatabases().stream().map(db -> db.getName());
        }
        dbNames.filter(this::isReplicable).forEach(databaseName -> {
            MyWritePermissionSupplier writePermissionSupplier = new MyWritePermissionSupplier(databaseName);
            CloneOptions options = new CloneOptions(true, true, true, false, databaseName, Collections.<String>emptySet(), writePermissionSupplier, (colName) -> replFilters.getCollectionPredicate().test(databaseName, colName), (collection, indexName, unique, keys) -> replFilters.getIndexPredicate().test(databaseName, collection, indexName, unique, keys));
            try {
                cloner.cloneDatabase(databaseName, remoteClient, server, options);
            } catch (MongoException ex) {
                throw new CloningException(ex);
            }
        });
    } finally {
        disableDataImportMode();
    }
}
Also used : RunnableTorodbService(com.torodb.core.services.RunnableTorodbService) ReadOplogTransaction(com.torodb.mongodb.repl.OplogManager.ReadOplogTransaction) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) RollbackException(com.torodb.core.transaction.RollbackException) Assisted(com.google.inject.assistedinject.Assisted) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) MongoDbRepl(com.torodb.mongodb.repl.guice.MongoDbRepl) MongoClient(com.eightkdata.mongowp.client.core.MongoClient) Empty(com.eightkdata.mongowp.server.api.tools.Empty) RollbackReplicationException(com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) OplogOperationUnsupported(com.eightkdata.mongowp.exceptions.OplogOperationUnsupported) ThreadFactory(java.util.concurrent.ThreadFactory) OplogFetcher(com.torodb.mongodb.repl.oplogreplier.fetcher.OplogFetcher) CancellationException(java.util.concurrent.CancellationException) CloningException(com.torodb.mongodb.utils.DbCloner.CloningException) ListDatabasesCommand(com.torodb.mongodb.commands.signatures.diagnostic.ListDatabasesCommand) OplogApplier(com.torodb.mongodb.repl.oplogreplier.OplogApplier) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Stream(java.util.stream.Stream) StopReplicationException(com.torodb.mongodb.repl.oplogreplier.StopReplicationException) OpTime(com.eightkdata.mongowp.OpTime) OplogStartMissingException(com.eightkdata.mongowp.exceptions.OplogStartMissingException) Supplier(com.google.common.base.Supplier) MongodServer(com.torodb.mongodb.core.MongodServer) UnreachableMongoServerException(com.eightkdata.mongowp.client.core.UnreachableMongoServerException) Inject(javax.inject.Inject) TorodConnection(com.torodb.torod.TorodConnection) NoSyncSourceFoundException(com.torodb.mongodb.repl.exceptions.NoSyncSourceFoundException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) MongoClientFactory(com.eightkdata.mongowp.client.core.MongoClientFactory) LimitedOplogFetcher(com.torodb.mongodb.repl.oplogreplier.fetcher.LimitedOplogFetcher) RemoteCommandResponse(com.eightkdata.mongowp.client.core.MongoConnection.RemoteCommandResponse) OplogManagerPersistException(com.torodb.mongodb.repl.OplogManager.OplogManagerPersistException) DbCloner(com.torodb.mongodb.utils.DbCloner) Supervisor(com.torodb.core.supervision.Supervisor) Nonnull(javax.annotation.Nonnull) CloneOptions(com.torodb.mongodb.utils.DbCloner.CloneOptions) SharedWriteTorodTransaction(com.torodb.torod.SharedWriteTorodTransaction) TorodbRunnableService(com.torodb.core.annotations.TorodbRunnableService) SupervisorDecision(com.torodb.core.supervision.SupervisorDecision) MongoCursor(com.eightkdata.mongowp.server.api.pojos.MongoCursor) UnexpectedOplogApplierException(com.torodb.mongodb.repl.oplogreplier.OplogApplier.UnexpectedOplogApplierException) UserException(com.torodb.core.exceptions.user.UserException) WriteOplogTransaction(com.torodb.mongodb.repl.OplogManager.WriteOplogTransaction) HostAndPort(com.google.common.net.HostAndPort) Status(com.eightkdata.mongowp.Status) TorodServer(com.torodb.torod.TorodServer) ListDatabasesReply(com.torodb.mongodb.commands.signatures.diagnostic.ListDatabasesCommand.ListDatabasesReply) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) CloningException(com.torodb.mongodb.utils.DbCloner.CloningException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) ListDatabasesReply(com.torodb.mongodb.commands.signatures.diagnostic.ListDatabasesCommand.ListDatabasesReply) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) CloneOptions(com.torodb.mongodb.utils.DbCloner.CloneOptions)

Aggregations

Collections (java.util.Collections)113 List (java.util.List)59 ArrayList (java.util.ArrayList)41 Test (org.junit.Test)39 Map (java.util.Map)37 Collectors (java.util.stream.Collectors)34 Arrays (java.util.Arrays)28 HashMap (java.util.HashMap)26 Set (java.util.Set)25 HashSet (java.util.HashSet)23 IOException (java.io.IOException)19 Collection (java.util.Collection)19 Optional (java.util.Optional)19 TimeUnit (java.util.concurrent.TimeUnit)16 URI (java.net.URI)13 Assert (org.junit.Assert)13 Function (java.util.function.Function)12 Stream (java.util.stream.Stream)12 Before (org.junit.Before)12 Logger (org.slf4j.Logger)12