Search in sources :

Example 96 with Nullable

use of javax.annotation.Nullable in project keywhiz by square.

the class ExpirationExtractor method expirationFromKeystore.

@Nullable
public static Instant expirationFromKeystore(String type, String password, byte[] content) {
    KeyStore ks;
    try {
        ks = KeyStore.getInstance(type);
    } catch (KeyStoreException e) {
        // Should never occur (assuming JCE is installed)
        throw Throwables.propagate(e);
    }
    try {
        ks.load(new ByteArrayInputStream(content), password.toCharArray());
    } catch (IOException | NoSuchAlgorithmException | CertificateException e) {
        // Failed to parse
        logger.info("Failed to parse keystore", e);
        return null;
    }
    Instant earliest = null;
    try {
        for (String alias : list(ks.aliases())) {
            Certificate[] chain = ks.getCertificateChain(alias);
            if (chain == null) {
                Certificate certificate = ks.getCertificate(alias);
                if (certificate == null) {
                    // No certs in this entry
                    continue;
                }
                chain = new Certificate[] { certificate };
            }
            for (Certificate cert : chain) {
                if (cert instanceof X509Certificate) {
                    X509Certificate c = (X509Certificate) cert;
                    if (earliest == null || c.getNotAfter().toInstant().isBefore(earliest)) {
                        earliest = c.getNotAfter().toInstant();
                    }
                }
            }
        }
    } catch (KeyStoreException e) {
        // Should never occur (ks was initialized)
        throw Throwables.propagate(e);
    }
    return earliest;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Instant(java.time.Instant) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Nullable(javax.annotation.Nullable)

Example 97 with Nullable

use of javax.annotation.Nullable in project commons by twitter.

the class CmdLineProcessor method processParsers.

private void processParsers(Set<? extends Element> elements) {
    TypeElement parserType = typeElement(Parser.class);
    for (Element element : elements) {
        if (element.getKind() != ElementKind.CLASS) {
            error("Found an @ArgParser annotation on a non-class %s", element);
        } else {
            TypeElement parser = (TypeElement) element;
            if (!isAssignable(parser, Parser.class)) {
                error("Found an @ArgParser annotation on a non-Parser %s", element);
                return;
            }
            @Nullable String parsedType = getTypeArgument(parser, parserType);
            if (parsedType != null) {
                String parserClassName = getBinaryName(parser);
                getBuilder(parserClassName).addParser(parsedType, getBinaryName(parser));
            }
        }
    }
}
Also used : TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Nullable(javax.annotation.Nullable) ArgParser(com.twitter.common.args.ArgParser) Parser(com.twitter.common.args.Parser)

Example 98 with Nullable

use of javax.annotation.Nullable in project titan by thinkaurelius.

the class FulgoraGraphComputer method submit.

@Override
public Future<ComputerResult> submit() {
    if (executed)
        throw Exceptions.computerHasAlreadyBeenSubmittedAVertexProgram();
    else
        executed = true;
    // it is not possible execute a computer if it has no vertex program nor mapreducers
    if (null == vertexProgram && mapReduces.isEmpty())
        throw GraphComputer.Exceptions.computerHasNoVertexProgramNorMapReducers();
    // it is possible to run mapreducers without a vertex program
    if (null != vertexProgram) {
        GraphComputerHelper.validateProgramOnComputer(this, vertexProgram);
        this.mapReduces.addAll(this.vertexProgram.getMapReducers());
    }
    // if the user didn't set desired persistence/resultgraph, then get from vertex program or else, no persistence
    this.persistMode = GraphComputerHelper.getPersistState(Optional.ofNullable(this.vertexProgram), Optional.ofNullable(this.persistMode));
    this.resultGraphMode = GraphComputerHelper.getResultGraphState(Optional.ofNullable(this.vertexProgram), Optional.ofNullable(this.resultGraphMode));
    // determine the legality persistence and result graph options
    if (!this.features().supportsResultGraphPersistCombination(this.resultGraphMode, this.persistMode))
        throw GraphComputer.Exceptions.resultGraphPersistCombinationNotSupported(this.resultGraphMode, this.persistMode);
    memory = new FulgoraMemory(vertexProgram, mapReduces);
    return CompletableFuture.<ComputerResult>supplyAsync(() -> {
        final long time = System.currentTimeMillis();
        if (null != vertexProgram) {
            // ##### Execute vertex program
            vertexMemory = new FulgoraVertexMemory(expectedNumVertices, graph.getIDManager(), vertexProgram);
            // execute the vertex program
            vertexProgram.setup(memory);
            memory.completeSubRound();
            for (int iteration = 1; ; iteration++) {
                vertexMemory.nextIteration(vertexProgram.getMessageScopes(memory));
                jobId = name + "#" + iteration;
                VertexProgramScanJob.Executor job = VertexProgramScanJob.getVertexProgramScanJob(graph, memory, vertexMemory, vertexProgram);
                StandardScanner.Builder scanBuilder = graph.getBackend().buildEdgeScanJob();
                scanBuilder.setJobId(jobId);
                scanBuilder.setNumProcessingThreads(numThreads);
                scanBuilder.setWorkBlockSize(readBatchSize);
                scanBuilder.setJob(job);
                PartitionedVertexProgramExecutor pvpe = new PartitionedVertexProgramExecutor(graph, memory, vertexMemory, vertexProgram);
                try {
                    //Iterates over all vertices and computes the vertex program on all non-partitioned vertices. For partitioned ones, the data is aggregated
                    ScanMetrics jobResult = scanBuilder.execute().get();
                    long failures = jobResult.get(ScanMetrics.Metric.FAILURE);
                    if (failures > 0) {
                        throw new TitanException("Failed to process [" + failures + "] vertices in vertex program iteration [" + iteration + "]. Computer is aborting.");
                    }
                    //Runs the vertex program on all aggregated, partitioned vertices.
                    pvpe.run(numThreads, jobResult);
                    failures = jobResult.getCustom(PartitionedVertexProgramExecutor.PARTITION_VERTEX_POSTFAIL);
                    if (failures > 0) {
                        throw new TitanException("Failed to process [" + failures + "] partitioned vertices in vertex program iteration [" + iteration + "]. Computer is aborting.");
                    }
                } catch (Exception e) {
                    throw new TitanException(e);
                }
                vertexMemory.completeIteration();
                memory.completeSubRound();
                try {
                    if (this.vertexProgram.terminate(this.memory)) {
                        break;
                    }
                } finally {
                    memory.incrIteration();
                    memory.completeSubRound();
                }
            }
        }
        // ##### Execute mapreduce jobs
        // Collect map jobs
        Map<MapReduce, FulgoraMapEmitter> mapJobs = new HashMap<>(mapReduces.size());
        for (MapReduce mapReduce : mapReduces) {
            if (mapReduce.doStage(MapReduce.Stage.MAP)) {
                FulgoraMapEmitter mapEmitter = new FulgoraMapEmitter<>(mapReduce.doStage(MapReduce.Stage.REDUCE));
                mapJobs.put(mapReduce, mapEmitter);
            }
        }
        // Execute map jobs
        jobId = name + "#map";
        VertexMapJob.Executor job = VertexMapJob.getVertexMapJob(graph, vertexMemory, mapJobs);
        StandardScanner.Builder scanBuilder = graph.getBackend().buildEdgeScanJob();
        scanBuilder.setJobId(jobId);
        scanBuilder.setNumProcessingThreads(numThreads);
        scanBuilder.setWorkBlockSize(readBatchSize);
        scanBuilder.setJob(job);
        try {
            ScanMetrics jobResult = scanBuilder.execute().get();
            long failures = jobResult.get(ScanMetrics.Metric.FAILURE);
            if (failures > 0) {
                throw new TitanException("Failed to process [" + failures + "] vertices in map phase. Computer is aborting.");
            }
            failures = jobResult.getCustom(VertexMapJob.MAP_JOB_FAILURE);
            if (failures > 0) {
                throw new TitanException("Failed to process [" + failures + "] individual map jobs. Computer is aborting.");
            }
        } catch (Exception e) {
            throw new TitanException(e);
        }
        // Execute reduce phase and add to memory
        for (Map.Entry<MapReduce, FulgoraMapEmitter> mapJob : mapJobs.entrySet()) {
            FulgoraMapEmitter<?, ?> mapEmitter = mapJob.getValue();
            MapReduce mapReduce = mapJob.getKey();
            // sort results if a map output sort is defined
            mapEmitter.complete(mapReduce);
            if (mapReduce.doStage(MapReduce.Stage.REDUCE)) {
                final FulgoraReduceEmitter<?, ?> reduceEmitter = new FulgoraReduceEmitter<>();
                try (WorkerPool workers = new WorkerPool(numThreads)) {
                    workers.submit(() -> mapReduce.workerStart(MapReduce.Stage.REDUCE));
                    for (final Map.Entry queueEntry : mapEmitter.reduceMap.entrySet()) {
                        workers.submit(() -> mapReduce.reduce(queueEntry.getKey(), ((Iterable) queueEntry.getValue()).iterator(), reduceEmitter));
                    }
                    workers.submit(() -> mapReduce.workerEnd(MapReduce.Stage.REDUCE));
                } catch (Exception e) {
                    throw new TitanException("Exception while executing reduce phase", e);
                }
                //                    mapEmitter.reduceMap.entrySet().parallelStream().forEach(entry -> mapReduce.reduce(entry.getKey(), entry.getValue().iterator(), reduceEmitter));
                // sort results if a reduce output sort is defined
                reduceEmitter.complete(mapReduce);
                mapReduce.addResultToMemory(this.memory, reduceEmitter.reduceQueue.iterator());
            } else {
                mapReduce.addResultToMemory(this.memory, mapEmitter.mapQueue.iterator());
            }
        }
        // #### Write mutated properties back into graph
        Graph resultgraph = graph;
        if (persistMode == Persist.NOTHING && resultGraphMode == ResultGraph.NEW) {
            resultgraph = EmptyGraph.instance();
        } else if (persistMode != Persist.NOTHING && vertexProgram != null && !vertexProgram.getElementComputeKeys().isEmpty()) {
            //First, create property keys in graph if they don't already exist
            TitanManagement mgmt = graph.openManagement();
            try {
                for (String key : vertexProgram.getElementComputeKeys()) {
                    if (!mgmt.containsPropertyKey(key))
                        log.warn("Property key [{}] is not part of the schema and will be created. It is advised to initialize all keys.", key);
                    mgmt.getOrCreatePropertyKey(key);
                }
                mgmt.commit();
            } finally {
                if (mgmt != null && mgmt.isOpen())
                    mgmt.rollback();
            }
            //TODO: Filter based on VertexProgram
            Map<Long, Map<String, Object>> mutatedProperties = Maps.transformValues(vertexMemory.getMutableVertexProperties(), new Function<Map<String, Object>, Map<String, Object>>() {

                @Nullable
                @Override
                public Map<String, Object> apply(@Nullable Map<String, Object> o) {
                    return Maps.filterKeys(o, s -> !NON_PERSISTING_KEYS.contains(s));
                }
            });
            if (resultGraphMode == ResultGraph.ORIGINAL) {
                AtomicInteger failures = new AtomicInteger(0);
                try (WorkerPool workers = new WorkerPool(numThreads)) {
                    List<Map.Entry<Long, Map<String, Object>>> subset = new ArrayList<>(writeBatchSize / vertexProgram.getElementComputeKeys().size());
                    int currentSize = 0;
                    for (Map.Entry<Long, Map<String, Object>> entry : mutatedProperties.entrySet()) {
                        subset.add(entry);
                        currentSize += entry.getValue().size();
                        if (currentSize >= writeBatchSize) {
                            workers.submit(new VertexPropertyWriter(subset, failures));
                            subset = new ArrayList<>(subset.size());
                            currentSize = 0;
                        }
                    }
                    if (!subset.isEmpty())
                        workers.submit(new VertexPropertyWriter(subset, failures));
                } catch (Exception e) {
                    throw new TitanException("Exception while attempting to persist result into graph", e);
                }
                if (failures.get() > 0)
                    throw new TitanException("Could not persist program results to graph. Check log for details.");
            } else if (resultGraphMode == ResultGraph.NEW) {
                resultgraph = graph.newTransaction();
                for (Map.Entry<Long, Map<String, Object>> vprop : mutatedProperties.entrySet()) {
                    Vertex v = resultgraph.vertices(vprop.getKey()).next();
                    for (Map.Entry<String, Object> prop : vprop.getValue().entrySet()) {
                        v.property(VertexProperty.Cardinality.single, prop.getKey(), prop.getValue());
                    }
                }
            }
        }
        // update runtime and return the newly computed graph
        this.memory.setRuntime(System.currentTimeMillis() - time);
        this.memory.complete();
        return new DefaultComputerResult(resultgraph, this.memory);
    });
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScanMetrics(com.thinkaurelius.titan.diskstorage.keycolumnvalue.scan.ScanMetrics) MapReduce(org.apache.tinkerpop.gremlin.process.computer.MapReduce) Function(com.google.common.base.Function) ComputerResult(org.apache.tinkerpop.gremlin.process.computer.ComputerResult) DefaultComputerResult(org.apache.tinkerpop.gremlin.process.computer.util.DefaultComputerResult) ArrayList(java.util.ArrayList) List(java.util.List) TitanException(com.thinkaurelius.titan.core.TitanException) WorkerPool(com.thinkaurelius.titan.graphdb.util.WorkerPool) Graph(org.apache.tinkerpop.gremlin.structure.Graph) EmptyGraph(org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph) StandardTitanGraph(com.thinkaurelius.titan.graphdb.database.StandardTitanGraph) StandardScanner(com.thinkaurelius.titan.diskstorage.keycolumnvalue.scan.StandardScanner) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultComputerResult(org.apache.tinkerpop.gremlin.process.computer.util.DefaultComputerResult) TitanException(com.thinkaurelius.titan.core.TitanException) HashMap(java.util.HashMap) Map(java.util.Map) TitanManagement(com.thinkaurelius.titan.core.schema.TitanManagement) Nullable(javax.annotation.Nullable)

Example 99 with Nullable

use of javax.annotation.Nullable in project commons by twitter.

the class Args method from.

/**
   * Loads arg info from the given sources in addition to the default compile-time configuration.
   *
   * @param filter A predicate to select fields with.
   * @param sources Classes or object instances to scan for {@link Arg} fields.
   * @return The args info describing all discovered {@link Arg args}.
   * @throws IOException If there was a problem loading the default Args configuration.
   */
public static ArgsInfo from(Predicate<Field> filter, Iterable<?> sources) throws IOException {
    Preconditions.checkNotNull(filter);
    Preconditions.checkNotNull(sources);
    Configuration configuration = Configuration.load();
    ArgsInfo staticInfo = Args.fromConfiguration(configuration, filter);
    final ImmutableSet.Builder<PositionalInfo<?>> positionalInfos = ImmutableSet.<PositionalInfo<?>>builder().addAll(staticInfo.getPositionalInfo().asSet());
    final ImmutableSet.Builder<OptionInfo<?>> optionInfos = ImmutableSet.<OptionInfo<?>>builder().addAll(staticInfo.getOptionInfos());
    for (Object source : sources) {
        Class<?> clazz = source instanceof Class ? (Class) source : source.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            if (filter.apply(field)) {
                boolean cmdLine = field.isAnnotationPresent(CmdLine.class);
                boolean positional = field.isAnnotationPresent(Positional.class);
                if (cmdLine && positional) {
                    throw new IllegalArgumentException("An Arg cannot be annotated with both @CmdLine and @Positional, found bad Arg " + "field: " + field);
                } else if (cmdLine) {
                    optionInfos.add(OptionInfo.createFromField(field, source));
                } else if (positional) {
                    positionalInfos.add(PositionalInfo.createFromField(field, source));
                }
            }
        }
    }
    @Nullable PositionalInfo<?> positionalInfo = Iterables.getOnlyElement(positionalInfos.build(), null);
    return new ArgsInfo(configuration, Optional.fromNullable(positionalInfo), optionInfos.build());
}
Also used : Configuration(com.twitter.common.args.apt.Configuration) Field(java.lang.reflect.Field) ImmutableSet(com.google.common.collect.ImmutableSet) Nullable(javax.annotation.Nullable)

Example 100 with Nullable

use of javax.annotation.Nullable in project commons by twitter.

the class CmdLineProcessor method processVerifiers.

private void processVerifiers(Set<? extends Element> elements) {
    TypeElement verifierType = typeElement(Verifier.class);
    TypeElement verifierForType = typeElement(VerifierFor.class);
    for (Element element : elements) {
        if (element.getKind() != ElementKind.CLASS) {
            error("Found a @VerifierFor annotation on a non-class %s", element);
        } else {
            TypeElement verifier = (TypeElement) element;
            if (!isAssignable(verifier, Verifier.class)) {
                error("Found a @Verifier annotation on a non-Verifier %s", element);
                return;
            }
            String verifierClassName = getBinaryName(verifier);
            @Nullable AnnotationMirror verifierFor = getAnnotationMirror(verifier, verifierForType);
            if (verifierFor != null) {
                @Nullable TypeElement verifyAnnotationType = getClassType(verifierFor, "value", null);
                if (verifyAnnotationType != null) {
                    @Nullable String verifiedType = getTypeArgument(verifier, verifierType);
                    if (verifiedType != null) {
                        String verifyAnnotationClassName = elementUtils.getBinaryName(verifyAnnotationType).toString();
                        getBuilder(verifierClassName).addVerifier(verifiedType, verifyAnnotationClassName, verifierClassName);
                    }
                }
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) Verifier(com.twitter.common.args.Verifier) Nullable(javax.annotation.Nullable)

Aggregations

Nullable (javax.annotation.Nullable)735 IOException (java.io.IOException)85 Test (org.junit.Test)59 Map (java.util.Map)58 Function (com.google.common.base.Function)57 List (java.util.List)55 ArrayList (java.util.ArrayList)43 File (java.io.File)42 HashMap (java.util.HashMap)37 ItemStack (net.minecraft.item.ItemStack)36 ImmutableList (com.google.common.collect.ImmutableList)32 SkyKey (com.google.devtools.build.skyframe.SkyKey)28 Nonnull (javax.annotation.Nonnull)28 ImmutableMap (com.google.common.collect.ImmutableMap)27 Predicate (com.google.common.base.Predicate)26 URL (java.net.URL)22 Label (com.google.devtools.build.lib.cmdline.Label)21 HashSet (java.util.HashSet)20 Collectors (java.util.stream.Collectors)15 TypeElement (javax.lang.model.element.TypeElement)15