|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not |
| 7 | + * use this file except in compliance with the License. A copy of the License is |
| 8 | + * located at |
| 9 | + * |
| 10 | + * http://aws.amazon.com/apache2.0 |
| 11 | + * |
| 12 | + * or in the "license" file accompanying this file. This file is distributed on |
| 13 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 14 | + * express or implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.uber.cadence.samples.compression; |
| 19 | + |
| 20 | +import com.uber.cadence.client.WorkflowClient; |
| 21 | +import com.uber.cadence.client.WorkflowClientOptions; |
| 22 | +import com.uber.cadence.converter.DataConverter; |
| 23 | +import com.uber.cadence.converter.JsonDataConverter; |
| 24 | +import com.uber.cadence.internal.compatibility.Thrift2ProtoAdapter; |
| 25 | +import com.uber.cadence.internal.compatibility.proto.serviceclient.IGrpcServiceStubs; |
| 26 | +import com.uber.cadence.samples.common.SampleConstants; |
| 27 | +import com.uber.cadence.worker.Worker; |
| 28 | +import com.uber.cadence.worker.WorkerFactory; |
| 29 | + |
| 30 | +/** |
| 31 | + * Hosts the gzip-compression sample worker. Constructs a {@link WorkflowClient} configured with |
| 32 | + * {@link CompressedJsonDataConverter} so every workflow input, output, and activity parameter is |
| 33 | + * transparently gzip-compressed in Cadence history. On startup it prints a stats banner showing the |
| 34 | + * before/after size of the sample payload so the benefit is visible at a glance. |
| 35 | + */ |
| 36 | +public final class CompressionWorker { |
| 37 | + |
| 38 | + private CompressionWorker() {} |
| 39 | + |
| 40 | + public static void main(String[] args) { |
| 41 | + DataConverter converter = new CompressedJsonDataConverter(); |
| 42 | + WorkflowClient client = |
| 43 | + WorkflowClient.newInstance( |
| 44 | + new Thrift2ProtoAdapter(IGrpcServiceStubs.newInstance()), |
| 45 | + WorkflowClientOptions.newBuilder() |
| 46 | + .setDomain(SampleConstants.DOMAIN) |
| 47 | + .setDataConverter(converter) |
| 48 | + .build()); |
| 49 | + |
| 50 | + WorkerFactory factory = WorkerFactory.newInstance(client); |
| 51 | + Worker worker = factory.newWorker(CompressedDataConverterWorkflow.TASK_LIST); |
| 52 | + worker.registerWorkflowImplementationTypes(CompressedDataConverterWorkflow.WorkflowImpl.class); |
| 53 | + worker.registerActivitiesImplementations(new CompressedDataConverterWorkflow.ActivitiesImpl()); |
| 54 | + factory.start(); |
| 55 | + |
| 56 | + printCompressionStats(converter); |
| 57 | + |
| 58 | + System.out.println( |
| 59 | + "CompressionWorker listening on \"" |
| 60 | + + CompressedDataConverterWorkflow.TASK_LIST |
| 61 | + + "\" (domain \"" |
| 62 | + + SampleConstants.DOMAIN |
| 63 | + + "\")."); |
| 64 | + |
| 65 | + Runtime.getRuntime().addShutdownHook(new Thread(factory::shutdown)); |
| 66 | + } |
| 67 | + |
| 68 | + private static void printCompressionStats(DataConverter converter) { |
| 69 | + CompressedDataConverterWorkflow.LargePayload payload = |
| 70 | + CompressedDataConverterWorkflow.createLargePayload(); |
| 71 | + byte[] originalJson = JsonDataConverter.getInstance().toData(payload); |
| 72 | + byte[] compressed = converter.toData(payload); |
| 73 | + int originalSize = originalJson == null ? 0 : originalJson.length; |
| 74 | + int compressedSize = compressed == null ? 0 : compressed.length; |
| 75 | + double pct = originalSize == 0 ? 0.0 : (1.0 - (double) compressedSize / originalSize) * 100.0; |
| 76 | + |
| 77 | + System.out.println(); |
| 78 | + System.out.println("=== Compression Sample Statistics ==="); |
| 79 | + System.out.printf( |
| 80 | + "Original JSON size: %d bytes (%.2f KB)%n", originalSize, originalSize / 1024.0); |
| 81 | + System.out.printf( |
| 82 | + "Compressed size: %d bytes (%.2f KB)%n", compressedSize, compressedSize / 1024.0); |
| 83 | + System.out.printf("Compression ratio: %.2f%% reduction%n", pct); |
| 84 | + System.out.printf( |
| 85 | + "Space saved: %d bytes (%.2f KB)%n", |
| 86 | + originalSize - compressedSize, (originalSize - compressedSize) / 1024.0); |
| 87 | + System.out.printf( |
| 88 | + "Start workflow: cadence --domain %s workflow start --tl %s --workflow_type %s --et 60%n", |
| 89 | + SampleConstants.DOMAIN, |
| 90 | + CompressedDataConverterWorkflow.TASK_LIST, |
| 91 | + CompressedDataConverterWorkflow.WORKFLOW_TYPE); |
| 92 | + System.out.println("====================================="); |
| 93 | + System.out.println(); |
| 94 | + } |
| 95 | +} |
0 commit comments