CSVBasedChurnModel.java 9.96 KB
Newer Older
Nils Richerzhagen's avatar
Nils Richerzhagen committed
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package de.tud.kom.p2psim.impl.churn;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import de.tud.kom.p2psim.api.churn.ChurnModel;
import de.tud.kom.p2psim.api.common.SimHost;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.impl.scenario.DefaultConfigurator;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;

/**
 * A simple churn model that reads a text file as input to set groups of nodes on- and offline.
 * Doing so the path to the text file has to be given in the config.
 * 
 * The file must have the following format:
 * 
 * group_name, time when to go online, time when to go offline, duration
 * 
 * The following example shows the format:
 * 
 * main_noise_peers_8 , 30min, 15min, 45min
 * main_noise_peers_8 , 1h, 2h, 1h
 * ...
 * 
 * @author Nils Richerzhagen
 * @version 1.0, Feb 18, 2015
 */
public class CSVBasedChurnModel implements ChurnModel {

	private String filename;
	
	private final String SEP = ",";
	
	private Map<String, LinkedList<ChurnInfo>> churnInfos;

	private Map<String, Boolean> usedForDowntime;
	

	/**
	 * 
	 * @param file
	 */
	@XMLConfigurableConstructor({ "file" })
	public CSVBasedChurnModel(String file) {
		this.filename = file;
		this.churnInfos = new LinkedHashMap<String, LinkedList<ChurnInfo>>();
		this.usedForDowntime = new LinkedHashMap<String, Boolean>();
	}

	@Override
	public long getNextUptime(SimHost host) {
		String groupId = host.getProperties().getGroupID();
		LinkedList<ChurnInfo> hostChurnInfos = churnInfos.get(groupId);
		
		if(usedForDowntime.containsKey(groupId) && usedForDowntime.get(groupId) == true){
			hostChurnInfos.removeFirst();
			usedForDowntime.put(groupId, false);
		}
		else
			usedForDowntime.put(groupId, false);
		
		if(hostChurnInfos.isEmpty())
			throw new AssertionError("No more churn infos left, is tht possible? " + host.getProperties().getGroupID());
		ChurnInfo churnInfo = hostChurnInfos.getFirst();
		if(churnInfo == null){
			throw new AssertionError("Seems to have not enough entries for group: " + host.getProperties().getGroupID());
		}
		

//		if(!notRemovedFirstThisRun){
//			notRemovedFirstThisRun = true;
//		}
		return churnInfo.getStartTime() - Simulator.getCurrentTime();
		
//		// the next interval should be an offline 
//		if(churnInfo.isOnline()){
//			return churnInfo.getDuration();
//		}
//		else{
//			try {
//				hostChurnInfos.removeFirst();
//			} catch (NoSuchElementException e) {
//				throw new AssertionError("Seems to have not enough entries for group: " + host.getProperties().getGroupID());
//			}
//			churnInfo = hostChurnInfos.getFirst();
//			if(churnInfo == null)
//			{
//				throw new AssertionError("Seems to have not enough entries for group: " + host.getProperties().getGroupID());
//			}
//			if(!churnInfo.isOnline()){
//				throw new AssertionError("There should be an online interval after the offline one: " + host.getProperties().getGroupID());
//			}
//			return churnInfo.getDuration();
//		}
	}

	@Override
	public long getNextDowntime(SimHost host) {
		String groupId = host.getProperties().getGroupID();
		LinkedList<ChurnInfo> hostChurnInfos = churnInfos.get(groupId);
		
		usedForDowntime.put(groupId, true);
		
//		if(usedForDowntime){
//			hostChurnInfos.removeFirst();
//			usedForDowntime = false;
//		}
		
		ChurnInfo churnInfo = hostChurnInfos.getFirst();
		if(churnInfo == null){
			throw new AssertionError("Seems to have not enough entries for group: " + host.getProperties().getGroupID());
		}
		// use churn info
		return churnInfo.getEndTime() - Simulator.getCurrentTime();
		
//		// the next interval should be an offline 
//		if(!churnInfo.isOnline()){
//			return churnInfo.getDuration();
//		}
//		else{
//			try {
//				hostChurnInfos.removeFirst();
//			} catch (NoSuchElementException e) {
//				throw new AssertionError("Seems to have not enough entries for group: " + host.getProperties().getGroupID());
//			}
//			churnInfo = hostChurnInfos.getFirst();
//			if(churnInfo == null){
//				throw new AssertionError("Seems to have not enough entries for group: " + host.getProperties().getGroupID());
//			}
//			if(churnInfo.isOnline()){
//				throw new AssertionError("There should be an offline interval after the previous online one: " + host.getProperties().getGroupID());
//			}
//			return churnInfo.getDuration();
//		}
	}

	@Override
	public void prepare(List<SimHost> churnHosts) {

		for (SimHost host : churnHosts) {
			for (SimNetInterface netI : host.getNetworkComponent()
					.getSimNetworkInterfaces()) {
				if (netI.isOnline()) {
					netI.goOffline();
				}
			}
		}
		parseTrace(filename);
	}

	private void parseTrace(String filename) {
		System.out.println("==============================");
		System.out.println("Reading trace from " + filename);

		/*
		 * This parser works for the following csv file structure.
		 * 
		 * groupID, startTime, endTime, duration, boolean (true/false)
		 * 
		 * groupID - describes the group id of the nodes that are affected
		 * 
		 * startTime - the time when the event should occur
		 * 
		 * endTime - Insanity Check
		 * 
		 * duration - how long should the event take
		 * 
		 * interChrunInterval - defines the time between each leave or join
		 * action in this group and this event. This is used as it makes the
		 * behaviour more realistic compared to a simple online/offline at the
		 * same time action.
		 * 
		 * boolean - defines if the group should be online or offline during this event
		 */
		BufferedReader csv = null;
		boolean entrySuccessfullyRead = false;
		int lines = 0;

		try {
			csv = new BufferedReader(new FileReader(filename));

			String previousGroupId = "";
			boolean firstGroup = true;
			long previousEndTime = 0;
			
			while (csv.ready()) {
				String line = csv.readLine();
				lines++;

				if (line.indexOf(SEP) > -1) {
					String[] parts = line.split(SEP);

						if (parts.length == 4) {
							try {
								String groupID = parts[0].replaceAll("\\s+","");
								int a = DefaultConfigurator.parseNumber("20m", Integer.class);
								long startTime = DefaultConfigurator.parseNumber(parts[1].replaceAll("\\s+",""), Long.class);
								long endTime = DefaultConfigurator.parseNumber(parts[2].replaceAll("\\s+",""), Long.class);
								long duration = DefaultConfigurator.parseNumber(parts[3].replaceAll("\\s+",""), Long.class);
//								boolean online = Boolean.parseBoolean(parts[4].replaceAll("\\s+", ""));		
								
//								String onlineString = parts[5].replaceAll("\\s+","");
								
								if(firstGroup){
									previousGroupId = groupID;
//									previousEndTime = startTime;
									firstGroup = false;
								}
								
								// new group id
								if(!previousGroupId.equals(groupID)){
									previousGroupId = groupID;
//									previousEndTime = startTime;
									previousEndTime = 0;
								}
									
								// Insanity Checks
//								if(startTime != previousEndTime){
								if(startTime < previousEndTime){
									throw new AssertionError("Wrong time in CSV for startTime as previousEndTime is larger.");
								}
								if(endTime - startTime != duration){
									throw new AssertionError("Duration must be the same as endTime - startTime");
								}
//								if((onlineString.equals("true") && !online)|| (onlineString.equals("false") && online)){									
//									throw new AssertionError("Boolean should be the same!");								
//								}
								
								LinkedList<ChurnInfo> infoList = churnInfos.get(groupID);
								if(infoList == null){
									infoList = new LinkedList<CSVBasedChurnModel.ChurnInfo>();
//									infoList.add(new ChurnInfo(startTime, online, endTime));
									infoList.add(new ChurnInfo(startTime, endTime));
									churnInfos.put(groupID, infoList);
								}
								else{
//									infoList.add(new ChurnInfo(startTime, online, endTime));
									infoList.add(new ChurnInfo(startTime, endTime));
								}
								
								entrySuccessfullyRead = true;

							} catch (NumberFormatException e) {
								// Ignore leading comments
								if (entrySuccessfullyRead) {
									// System.err.println("CSV ParseError " +
									// line);
								}
							}
						} else {
							throw new AssertionError("To many/few columns in CSV.");
						}
					}
				}
			
		}
		catch (Exception e) {
			System.err.println("Could not open " + filename);
			throw new RuntimeException("Could not open " + filename);
		}
		finally {
			if (csv != null) {
				try {
					csv.close();
				} catch (IOException e) {
					//
				}
			}
		}

	}
	
	
	private class ChurnInfo {
	
		long startTime;
		long endTime;
//		boolean online;
				
		public ChurnInfo(long startTime, long endTime){
//		public ChurnInfo(long startTime, boolean online, long endTime){
			this.startTime = startTime;
//			this.online = online;
			this.endTime = endTime;
		}
		
		public long getEndTime(){
			return endTime;
		}
		public long getDuration(){
			// TODO introduce skew/offset
			return (endTime - startTime);
		}
		
		public long getStartTime(){
			return startTime;
		}
//		
//		/**
//		 * Returns if this churn interval stands for an online interval
//		 * @return
//		 */
//		public boolean isOnline(){
//			return this.online;
//		}
	}

}