public void finish() { if (state == State.NOT_STARTED) { throw new NinjaException("Can't finish operation, previous state was " + State.NOT_STARTED); } debug("Operation: finished"); finishTime = System.currentTimeMillis(); state = State.FINISHED; }
@Override public void handleGlobalError(OperationResult currentResult) { operation.finish(); } };
public String print() { StringBuilder sb = new StringBuilder(); sb.append("Processed: "); sb.append(totalCount.get()); sb.append(", error: "); sb.append(errorCount.get()); sb.append(", skipped: "); sb.append(skippedCount.get()); sb.append(", avg: "); sb.append(NinjaUtils.DECIMAL_FORMAT.format(getAvgRequestPerSecond())); sb.append("obj/s"); return sb.toString(); }
protected boolean shouldConsumerStop() { if (operation.isFinished()) { return true; } if (operation.isStarted()) { return false; } if (operation.isProducerFinished() && !queue.isEmpty()) { return false; } return true; }
protected void handleResultOnFinish(OperationStatus operation, String finishMessage) { OperationResult result = operation.getResult(); result.recomputeStatus(); if (result.isAcceptable()) { log.info("{} in {}s. {}", finishMessage, NinjaUtils.DECIMAL_FORMAT.format(operation.getTotalTime()), operation.print()); } else { log.error("{} in {}s with some problems, reason: {}. {}", finishMessage, NinjaUtils.DECIMAL_FORMAT.format(operation.getTotalTime()), result.getMessage(), operation.print()); if (context.isVerbose()) { log.error("Full result\n{}", result.debugDumpLazily()); } } }
writer.flush(); operation.incrementTotal(); } catch (Exception ex) { log.error("Couldn't store object {}, reason: {}", ex, object, ex.getMessage()); operation.incrementError(); operation.finish();
operation.start(); if (!operation.isFinished()) { operation.producerFinish();
operation.incrementSkipped(); return true; case STOP: repository.deleteObject(prismObject.getCompileTimeClass(), prismObject.getOid(), operationResult); operation.incrementTotal(); } catch (ObjectNotFoundException ex) { operation.incrementError();
repository.searchObjectsIterative(type.getClassDefinition(), query, handler, optionsBuilder.build(), true, operation.getResult()); } catch (SchemaException ex) { log.error("Unexpected exception, reason: {}", ex, ex.getMessage()); if (!operation.isFinished()) { operation.producerFinish();
@Override public void execute() throws Exception { OperationResult result = new OperationResult(getOperationName()); OperationStatus operation = new OperationStatus(context, result); // "+ 2" will be used for consumer and progress reporter ExecutorService executor = Executors.newFixedThreadPool(options.getMultiThread() + 2); BlockingQueue<PrismObject> queue = new LinkedBlockingQueue<>(QUEUE_CAPACITY_PER_THREAD * options.getMultiThread()); List<SearchProducerWorker> producers = createProducers(queue, operation); log.info("Starting " + getOperationShortName()); operation.start(); // execute as many producers as there are threads for them for (int i = 0; i < producers.size() && i < options.getMultiThread(); i++) { executor.execute(producers.get(i)); } Thread.sleep(CONSUMERS_WAIT_FOR_START); executor.execute(new ProgressReporterWorker(context, options, queue, operation)); Runnable consumer = createConsumer(queue, operation); executor.execute(consumer); // execute rest of the producers for (int i = options.getMultiThread(); i < producers.size(); i++) { executor.execute(producers.get(i)); } executor.shutdown(); executor.awaitTermination(NinjaUtils.WAIT_FOR_EXECUTOR_FINISH, TimeUnit.DAYS); handleResultOnFinish(operation, "Finished " + getOperationShortName()); }
@Override public <T extends Objectable> EventResult postMarshall(PrismObject<T> object, Element objectElement, OperationResult objectResult) { try { if (filter != null) { boolean match = ObjectQuery.match(object, filter, matchingRuleRegistry); if (!match) { operation.incrementSkipped(); return EventResult.skipObject("Object doesn't match filter"); } } if (!matchSelectedType(object.getCompileTimeClass())) { operation.incrementSkipped(); return EventResult.skipObject("Type doesn't match"); } queue.put(object); } catch (Exception ex) { throw new NinjaException("Couldn't import object, reason: " + ex.getMessage(), ex); } return stopAfterFound ? EventResult.skipObject() : EventResult.cont(); }
@Override public void execute() throws Exception { OperationResult result = new OperationResult(OPERATION_IMPORT); OperationStatus progress = new OperationStatus(context, result); BlockingQueue<PrismObject> queue = new LinkedBlockingQueue<>(QUEUE_CAPACITY_PER_THREAD * options.getMultiThread()); // "+ 2" will be used for producer and progress reporter ExecutorService executor = Executors.newFixedThreadPool(options.getMultiThread() + 2); ImportProducerWorker producer; if (options.getOid() != null) { InOidFilter filter = context.getPrismContext().queryFactory().createInOid(options.getOid()); producer = importByFilter(filter, true, queue, progress); } else { ObjectFilter filter = NinjaUtils.createObjectFilter(options.getFilter(), context, ObjectType.class); // todo ok? (ObjectType) producer = importByFilter(filter, false, queue, progress); } executor.execute(producer); Thread.sleep(CONSUMERS_WAIT_FOR_START); executor.execute(new ProgressReporterWorker(context, options, queue, progress)); List<ImportConsumerWorker> consumers = createConsumers(queue, progress); consumers.stream().forEach(c -> executor.execute(c)); executor.shutdown(); executor.awaitTermination(NinjaUtils.WAIT_FOR_EXECUTOR_FINISH, TimeUnit.DAYS); handleResultOnFinish(progress, "Import finished"); }
validator.setValidateSchema(false); OperationResult result = operation.getResult();
repository.addObject(object, opts, new OperationResult("Import object")); operation.incrementTotal(); } catch (Exception ex) { context.getLog().error("Couldn't add object {}, reason: {}", ex, object, ex.getMessage()); operation.incrementError(); operation.finish();
private void deleteByFilter(ObjectQuery query) throws SchemaException, IOException { OperationResult result = new OperationResult(OPERATION_DELETE); OperationStatus operation = new OperationStatus(context, result); operation.start(); log.info("Starting delete"); ObjectTypes type = options.getType(); if (type != null) { deleteByFilter(type, query, operation, result); } else { for (ObjectTypes t : ObjectTypes.values()) { if (Modifier.isAbstract(t.getClassDefinition().getModifiers())) { continue; } deleteByFilter(t, query, operation, result); } } handleResultOnFinish(operation, "Delete finished"); }
public void start() { if (state != State.NOT_STARTED) { throw new NinjaException("Can't start operation, previous state is was not " + State.NOT_STARTED); } debug("Operation: started"); startTime = System.currentTimeMillis(); state = State.STARTED; }
public void producerFinish() { if (state != State.STARTED) { throw new NinjaException("Can't set state " + State.PRODUCER_FINISHED + " for operation, previous state is was not " + State.STARTED); } debug("Operation: producer finished"); state = State.PRODUCER_FINISHED; }