forked from skylinemarketing/kafka-streams-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InventoryService.java
152 lines (131 loc) · 6.86 KB
/
InventoryService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package io.confluent.examples.streams.microservices;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.Joined;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Produced;
import org.apache.kafka.streams.kstream.Transformer;
import org.apache.kafka.streams.processor.ProcessorContext;
import org.apache.kafka.streams.state.KeyValueStore;
import org.apache.kafka.streams.state.StoreBuilder;
import org.apache.kafka.streams.state.Stores;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import io.confluent.examples.streams.avro.microservices.Order;
import io.confluent.examples.streams.avro.microservices.OrderState;
import io.confluent.examples.streams.avro.microservices.OrderValidation;
import io.confluent.examples.streams.avro.microservices.Product;
import io.confluent.examples.streams.microservices.domain.Schemas.Topics;
import io.confluent.examples.streams.microservices.util.MicroserviceUtils;
import static io.confluent.examples.streams.avro.microservices.OrderValidationResult.FAIL;
import static io.confluent.examples.streams.avro.microservices.OrderValidationResult.PASS;
import static io.confluent.examples.streams.avro.microservices.OrderValidationType.INVENTORY_CHECK;
import static io.confluent.examples.streams.microservices.util.MicroserviceUtils.addShutdownHookAndBlock;
import static io.confluent.examples.streams.microservices.util.MicroserviceUtils.parseArgsAndConfigure;
/**
* This service validates incoming orders to ensure there is sufficient stock to
* fulfill them. This validation process considers both the inventory in the warehouse
* as well as a set "reserved" items which is maintained by this service. Reserved
* items are those that are in the warehouse, but have been allocated to a pending
* order.
* <p>
* Currently there is nothing implemented that decrements the reserved items. This
* would happen, inside this service, in response to an order being shipped.
*/
public class InventoryService implements Service {
private static final Logger log = LoggerFactory.getLogger(InventoryService.class);
public static final String SERVICE_APP_ID = "InventoryService";
public static final String RESERVED_STOCK_STORE_NAME = "store-of-reserved-stock";
private KafkaStreams streams;
@Override
public void start(final String bootstrapServers, final String stateDir) {
streams = processStreams(bootstrapServers, stateDir);
streams.cleanUp(); //don't do this in prod as it clears your state stores
streams.start();
log.info("Started Service " + getClass().getSimpleName());
}
@Override
public void stop() {
if (streams != null) {
streams.close();
}
}
private KafkaStreams processStreams(final String bootstrapServers, final String stateDir) {
//Latch onto instances of the orders and inventory topics
final StreamsBuilder builder = new StreamsBuilder();
final KStream<String, Order> orders = builder
.stream(Topics.ORDERS.name(),
Consumed.with(Topics.ORDERS.keySerde(), Topics.ORDERS.valueSerde()));
final KTable<Product, Integer> warehouseInventory = builder
.table(Topics.WAREHOUSE_INVENTORY.name(), Consumed
.with(Topics.WAREHOUSE_INVENTORY.keySerde(), Topics.WAREHOUSE_INVENTORY.valueSerde()));
//Create a store to reserve inventory whilst the order is processed.
//This will be prepopulated from Kafka before the service starts processing
final StoreBuilder reservedStock = Stores
.keyValueStoreBuilder(Stores.persistentKeyValueStore(RESERVED_STOCK_STORE_NAME),
Topics.WAREHOUSE_INVENTORY.keySerde(), Serdes.Long())
.withLoggingEnabled(new HashMap<>());
builder.addStateStore(reservedStock);
//First change orders stream to be keyed by Product (so we can join with warehouse inventory)
orders.selectKey((id, order) -> order.getProduct())
//Limit to newly created orders
.filter((id, order) -> OrderState.CREATED.equals(order.getState()))
//Join Orders to Inventory so we can compare each order to its corresponding stock value
.join(warehouseInventory, KeyValue::new, Joined.with(Topics.WAREHOUSE_INVENTORY.keySerde(),
Topics.ORDERS.valueSerde(), Serdes.Integer()))
//Validate the order based on how much stock we have both in the warehouse and locally 'reserved' stock
.transform(InventoryValidator::new, RESERVED_STOCK_STORE_NAME)
//Push the result into the Order Validations topic
.to(Topics.ORDER_VALIDATIONS.name(), Produced.with(Topics.ORDER_VALIDATIONS.keySerde(),
Topics.ORDER_VALIDATIONS.valueSerde()));
return new KafkaStreams(builder.build(),
MicroserviceUtils.baseStreamsConfig(bootstrapServers, stateDir, SERVICE_APP_ID));
}
private static class InventoryValidator implements
Transformer<Product, KeyValue<Order, Integer>, KeyValue<String, OrderValidation>> {
private KeyValueStore<Product, Long> reservedStocksStore;
@Override
@SuppressWarnings("unchecked")
public void init(final ProcessorContext context) {
reservedStocksStore = (KeyValueStore<Product, Long>) context
.getStateStore(RESERVED_STOCK_STORE_NAME);
}
@Override
public KeyValue<String, OrderValidation> transform(final Product productId,
final KeyValue<Order, Integer> orderAndStock) {
//Process each order/inventory pair one at a time
final OrderValidation validated;
final Order order = orderAndStock.key;
final Integer warehouseStockCount = orderAndStock.value;
//Look up locally 'reserved' stock from our state store
Long reserved = reservedStocksStore.get(order.getProduct());
if (reserved == null) {
reserved = 0L;
}
//If there is enough stock available (considering both warehouse inventory and reserved stock) validate the order
if (warehouseStockCount - reserved - order.getQuantity() >= 0) {
//reserve the stock by adding it to the 'reserved' store
reservedStocksStore.put(order.getProduct(), reserved + order.getQuantity());
//validate the order
validated = new OrderValidation(order.getId(), INVENTORY_CHECK, PASS);
} else {
//fail the order
validated = new OrderValidation(order.getId(), INVENTORY_CHECK, FAIL);
}
return KeyValue.pair(validated.getOrderId(), validated);
}
@Override
public void close() {
}
}
public static void main(final String[] args) throws Exception {
final InventoryService service = new InventoryService();
service.start(parseArgsAndConfigure(args), "/tmp/kafka-streams");
addShutdownHookAndBlock(service);
}
}