Github user srdo commented on a diff in the pull request:

    https://github.com/apache/storm/pull/2218#discussion_r129113343
  
    --- Diff: 
examples/storm-starter/src/jvm/org/apache/storm/starter/PersistentWindowingTopology.java
 ---
    @@ -0,0 +1,176 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + * http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.storm.starter;
    +
    +import static org.apache.storm.topology.base.BaseWindowedBolt.Duration;
    +
    +import java.util.Iterator;
    +import java.util.Map;
    +import java.util.concurrent.TimeUnit;
    +import org.apache.storm.Config;
    +import org.apache.storm.StormSubmitter;
    +import org.apache.storm.starter.spout.RandomIntegerSpout;
    +import org.apache.storm.state.KeyValueState;
    +import org.apache.storm.streams.Pair;
    +import org.apache.storm.task.OutputCollector;
    +import org.apache.storm.task.TopologyContext;
    +import org.apache.storm.topology.OutputFieldsDeclarer;
    +import org.apache.storm.topology.TopologyBuilder;
    +import org.apache.storm.topology.base.BaseStatefulWindowedBolt;
    +import org.apache.storm.tuple.Fields;
    +import org.apache.storm.tuple.Tuple;
    +import org.apache.storm.tuple.Values;
    +import org.apache.storm.windowing.TupleWindow;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +/**
    + * An example that demonstrates the usage of {@link 
org.apache.storm.topology.IStatefulWindowedBolt} with window
    + * persistence.
    + * <p>
    + * The framework automatically checkpoints the tuples in window along with 
the bolt's state and restores the same
    + * during restarts.
    + * </p>
    + *
    + * <p>
    + * This topology uses 'redis' for state persistence, so you should also 
start a redis instance before deploying.
    + * If you are running in local mode you can just start a redis server 
locally which will be used for storing the state. The default
    + * RedisKeyValueStateProvider parameters can be overridden by setting 
"topology.state.provider.config", for e.g.
    + * <pre>
    + * {
    + *   "jedisPoolConfig": {
    + *     "host": "redis-server-host",
    + *     "port": 6379,
    + *     "timeout": 2000,
    + *     "database": 0,
    + *     "password": "xyz"
    + *   }
    + * }
    + * </pre>
    + * </p>
    + */
    +public class PersistentWindowingTopology {
    +    private static final Logger LOG = 
LoggerFactory.getLogger(PersistentWindowingTopology.class);
    +
    +    // wrapper to hold global and window averages
    +    private static class Averages {
    +        private final double global;
    +        private final double window;
    +
    +        Averages(double global, double window) {
    +            this.global = global;
    +            this.window = window;
    +        }
    +
    +        @Override
    +        public String toString() {
    +            return "Averages{" + "global=" + String.format("%.2f", global) 
+ ", window=" + String.format("%.2f", window) + '}';
    +        }
    +    }
    +
    +    /**
    +     * A bolt that uses stateful persistence to store the windows along 
with the state (global avg).
    +     */
    +    private static class AvgBolt extends 
BaseStatefulWindowedBolt<KeyValueState<String, Pair<Long, Long>>> {
    +        private static final String STATE_KEY = "avg";
    +
    +        private OutputCollector collector;
    +        private KeyValueState<String, Pair<Long, Long>> state;
    +        private Pair<Long, Long> globalAvg;
    +
    +        @Override
    +        public void prepare(Map<String, Object> topoConf, TopologyContext 
context, OutputCollector collector) {
    +            this.collector = collector;
    +        }
    +
    +        @Override
    +        public void initState(KeyValueState<String, Pair<Long, Long>> 
state) {
    +            this.state = state;
    +            globalAvg = state.get(STATE_KEY, Pair.of(0L, 0L));
    +            LOG.info("initState with global avg [" + (double) globalAvg._1 
/ globalAvg._2 + "]");
    --- End diff --
    
    Nit: Consider using the getters instead of _1 and _2. It's a little weird 
that those fields are public when they also have getters IMO.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to