PageRenderTime 53ms CodeModel.GetById 8ms app.highlight 40ms RepoModel.GetById 2ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/examples/LongRunningTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 248 lines | 209 code | 22 blank | 17 comment | 17 complexity | 416b0c21d3bbc4888aa383d7b0e02149 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.examples;
 18
 19import com.hazelcast.core.Hazelcast;
 20import com.hazelcast.core.HazelcastInstance;
 21import com.hazelcast.util.Clock;
 22
 23import java.util.List;
 24import java.util.Map;
 25import java.util.concurrent.CopyOnWriteArrayList;
 26import java.util.concurrent.ExecutorService;
 27import java.util.concurrent.Executors;
 28import java.util.concurrent.TimeUnit;
 29import java.util.concurrent.atomic.AtomicLong;
 30import java.util.logging.Level;
 31import java.util.logging.Logger;
 32
 33public class LongRunningTest {
 34
 35    private static final int STATS_SECONDS = 10;
 36    private List<TheNode> nodes = new CopyOnWriteArrayList<TheNode>();
 37    private int nodeIdGen = 0;
 38    private final Logger logger = Logger.getLogger(LongRunningTest.class.getName());
 39    private int starts, stops, restarts = 0;
 40
 41    public static void main(String[] args) {
 42        LongRunningTest t = new LongRunningTest();
 43        t.run();
 44    }
 45
 46    public void run() {
 47        Runtime.getRuntime().addShutdownHook(new Thread() {
 48            public void run() {
 49                log("Shutting down " + nodes.size());
 50                while (nodes.size() > 0) {
 51                    removeNode();
 52                }
 53            }
 54        });
 55        while (true) {
 56            if (nodes.size() > 4) {
 57                removeNode();
 58            } else if (nodes.size() == 0) {
 59                addNode();
 60                addNode();
 61                addNode();
 62            } else if (nodes.size() < 2) {
 63                addNode();
 64            } else {
 65                int action = random(3);
 66                switch (action) {
 67                    case 0:
 68                        removeNode();
 69                        break;
 70                    case 1:
 71                        addNode();
 72                        break;
 73                    case 2:
 74                        restartNode();
 75                        break;
 76                }
 77            }
 78            try {
 79                int nextSeconds = random(60, 260);
 80                log("Next Action after " + nextSeconds + " seconds.");
 81                log("members:" + nodes.size() + ", starts: " + starts + ", stops:" + stops + ", restart:" + restarts);
 82                //noinspection BusyWait
 83                Thread.sleep(nextSeconds * 1000);
 84            } catch (InterruptedException e) {
 85            }
 86        }
 87    }
 88
 89    void log(Object obj) {
 90        logger.log(Level.INFO, "LRT-" + obj);
 91    }
 92
 93    void addNode() {
 94        starts++;
 95        int entryCount = random(10000);
 96        int threadCount = random(10, 50);
 97        int valueSizeMax = (entryCount < 1000) ? 50000 : 1000;
 98        int valueSize = random(10, valueSizeMax);
 99        TheNode node = new TheNode(nodeIdGen++, entryCount, threadCount, valueSize);
100        nodes.add(node);
101        node.start();
102        log("Started " + node);
103    }
104
105    void restartNode() {
106        restarts++;
107        log("Restarting...");
108        removeNode();
109        try {
110            Thread.sleep(random(10) * 1000);
111        } catch (InterruptedException e) {
112        }
113        addNode();
114    }
115
116    void removeNode() {
117        stops++;
118        TheNode node = nodes.remove(random(nodes.size()));
119        node.stop();
120        log("Stopped " + node);
121    }
122
123    int random(int length) {
124        return ((int) (Math.random() * 10000000) % length);
125    }
126
127    int random(int from, int to) {
128        double diff = (to - from);
129        return (int) (diff * Math.random() + from);
130    }
131
132    class TheNode {
133        final int entryCount;
134        final int threadCount;
135        final int valueSize;
136        final int nodeId;
137        final long createTime;
138        final ExecutorService es;
139        final ExecutorService esStats;
140        final HazelcastInstance hazelcast;
141        volatile boolean running = true;
142
143        TheNode(int nodeId, int entryCount, int threadCount, int valueSize) {
144            this.entryCount = entryCount;
145            this.threadCount = threadCount;
146            this.valueSize = valueSize;
147            this.nodeId = nodeId;
148            es = Executors.newFixedThreadPool(threadCount);
149            hazelcast = Hazelcast.newHazelcastInstance(null);
150            esStats = Executors.newSingleThreadExecutor();
151            createTime = Clock.currentTimeMillis();
152        }
153
154        public void stop() {
155            try {
156                running = false;
157                es.shutdown();
158                es.awaitTermination(10, TimeUnit.SECONDS);
159                esStats.shutdown();
160                hazelcast.shutdown();
161            } catch (Throwable t) {
162                t.printStackTrace();
163            }
164        }
165
166        public void start() {
167            final Stats stats = new Stats();
168            for (int i = 0; i < threadCount; i++) {
169                es.submit(new Runnable() {
170                    public void run() {
171                        Map<String, byte[]> map = hazelcast.getMap("default");
172                        while (running) {
173                            try {
174                                int key = (int) (Math.random() * entryCount);
175                                int operation = ((int) (Math.random() * 100)) % 10;
176                                if (operation < 4) {
177                                    map.put(String.valueOf(key), new byte[valueSize]);
178                                    stats.mapPuts.incrementAndGet();
179                                } else if (operation < 8) {
180                                    map.get(String.valueOf(key));
181                                    stats.mapGets.incrementAndGet();
182                                } else {
183                                    map.remove(String.valueOf(key));
184                                    stats.mapRemoves.incrementAndGet();
185                                }
186                            } catch (Throwable ignored) {
187                            }
188                        }
189                    }
190                });
191            }
192            esStats.submit(new Runnable() {
193                public void run() {
194                    while (running) {
195                        try {
196                            //noinspection BusyWait
197                            Thread.sleep(STATS_SECONDS * 1000);
198                            int clusterSize = hazelcast.getCluster().getMembers().size();
199                            Stats currentStats = stats.getAndReset();
200                            log("Cluster size: " + clusterSize + ", Operations per Second: "
201                                    + (currentStats.total() / STATS_SECONDS));
202                        } catch (Exception e) {
203                            e.printStackTrace();
204                        }
205                    }
206                }
207            });
208        }
209
210        @Override
211        public String toString() {
212            return "TheNode{" +
213                    "nodeId=" + nodeId +
214                    ", entryCount=" + entryCount +
215                    ", threadCount=" + threadCount +
216                    ", valueSize=" + valueSize +
217                    ", liveSeconds=" + ((Clock.currentTimeMillis() - createTime) / 1000) +
218                    ", running=" + running +
219                    '}';
220        }
221    }
222
223    class Stats {
224        public AtomicLong mapPuts = new AtomicLong();
225        public AtomicLong mapGets = new AtomicLong();
226        public AtomicLong mapRemoves = new AtomicLong();
227
228        public Stats getAndReset() {
229            long mapPutsNow = mapPuts.getAndSet(0);
230            long mapGetsNow = mapGets.getAndSet(0);
231            long mapRemovesNow = mapRemoves.getAndSet(0);
232            Stats newOne = new Stats();
233            newOne.mapPuts.set(mapPutsNow);
234            newOne.mapGets.set(mapGetsNow);
235            newOne.mapRemoves.set(mapRemovesNow);
236            return newOne;
237        }
238
239        public long total() {
240            return mapPuts.get() + mapGets.get() + mapRemoves.get();
241        }
242
243        public String toString() {
244            return "total= " + total() + ", puts:" + mapPuts.get() + ", gets:" + mapGets.get()
245                    + ", remove:" + mapRemoves.get();
246        }
247    }
248}