summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/maintenance/ImageValidCheck.java
blob: b544a7085420572850450994602f316cf93e20d0 (plain) (blame)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package org.openslx.bwlp.sat.maintenance;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.database.mappers.DbImage;
import org.openslx.bwlp.sat.database.mappers.DbImageBlock;
import org.openslx.bwlp.sat.database.models.ImageVersionMeta;
import org.openslx.bwlp.sat.database.models.LocalImageVersion;
import org.openslx.bwlp.sat.util.FileSystem;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.filetransfer.util.ChunkStatus;
import org.openslx.filetransfer.util.FileChunk;
import org.openslx.filetransfer.util.HashChecker;
import org.openslx.filetransfer.util.HashChecker.HashCheckCallback;
import org.openslx.filetransfer.util.HashChecker.HashResult;
import org.openslx.filetransfer.util.StandaloneFileChunk;
import org.openslx.util.ThriftUtil;
import org.openslx.util.TimeoutHashMap;
import org.openslx.util.Util;

public class ImageValidCheck implements Runnable {

	public static enum CheckResult {
		
		QUEUED(0),
		NO_SUCH_JOB(0),
		WAITING_FOR_STORE(1),
		WORKING(2),
		DONE(100),
		VERSION_EXPIRED(100),
		DATABASE_PATH_MISSING(100),
		DATABASE_PATH_INVALID(100),
		FILE_NOT_FOUND(100),
		FILE_ACCESS_ERROR(100),
		FILE_SIZE_MISMATCH(100),
		FILE_CORRUPT(100),
		UNKNOWN_VERSIONID(100),
		INTERNAL_ERROR(100);
		public final int stage;
		private CheckResult(int stage) {
			this.stage = stage;
		}
	}
	
	public static enum SubmitResult {
		QUEUED,
		NULL_POINTER_EXCEPTION,
		ALREADY_IN_PROGRESS,
		TOO_MANY_QUEUED_JOBS,
		REJECTED_BY_SCHEDULER,
	}

	private static final Logger LOGGER = Logger.getLogger(ImageValidCheck.class);
	
	private static final int MAX_CONCURRENT_CHECKS = 1;

	private static Queue<ImageValidCheck> queue = new LinkedList<>();
	private static Map<String, ImageValidCheck> inProgress = new HashMap<>();
	private static TimeoutHashMap<String, ImageValidCheck> done = new TimeoutHashMap<>(
			TimeUnit.MINUTES.toMillis(60));

	private final String versionId;
	private final boolean integrity;
	private final boolean updateState;

	/**
	 * Status of this check job. Must never be null.
	 */
	private CheckResult result = CheckResult.QUEUED;


	// Hash checking

	private static final HashChecker hashChecker;

	static {
		long maxMem = Runtime.getRuntime().maxMemory() / (1024 * 1024);
		int hashQueueLen;
		if (maxMem < 1200) {
			hashQueueLen = 1;
		} else {
			hashQueueLen = 2;
		}
		HashChecker hc;
		try {
			hc = new HashChecker("SHA-1", hashQueueLen);
		} catch (NoSuchAlgorithmException e) {
			hc = null;
		}
		hashChecker = hc;
	}

	// End hash checking

	public static SubmitResult check(String versionId, boolean integrity, boolean updateState) {
		if (versionId == null)
			return SubmitResult.NULL_POINTER_EXCEPTION;
		synchronized (inProgress) {
			synchronized (done) {
				if (done.containsKey(versionId)) {
					done.remove(versionId);
				}
			}
			if (inProgress.containsKey(versionId))
				return SubmitResult.ALREADY_IN_PROGRESS;
			if (inProgress.size() >= MAX_CONCURRENT_CHECKS) {
				if (queue.size() > 1000) {
					return SubmitResult.TOO_MANY_QUEUED_JOBS;
				}
				queue.add(new ImageValidCheck(versionId, integrity, updateState));
				return SubmitResult.QUEUED;
			}
			ImageValidCheck check = new ImageValidCheck(versionId, integrity, updateState);
			if (Maintenance.trySubmit(check)) {
				inProgress.put(versionId, check);
				return SubmitResult.QUEUED;
			}
		}
		return SubmitResult.REJECTED_BY_SCHEDULER;
	}

	public static void checkForWork() {
		synchronized (inProgress) {
			while (inProgress.size() < MAX_CONCURRENT_CHECKS && !queue.isEmpty()) {
				ImageValidCheck check = queue.poll();
				if (check == null)
					break;
				if (inProgress.containsKey(check.versionId))
					continue; // Already checking this version, try next in queue
				if (Maintenance.trySubmit(check)) {
					inProgress.put(check.versionId, check);
				} else {
					if (!queue.offer(check)) {
						LOGGER.warn("Dropped queued check for image version " + check.versionId);
					}
					// Scheduler didn't accept job - don't try remaining queue
					break;
				}
			}
		}
	}

	/**
	 * Get current status of check for given versionId.
	 * Never returns null.
	 * 
	 * @param versionId VERSIONSID DES IMAGES
	 * @return state/result
	 */
	public static CheckResult getStatus(String versionId) {
		ImageValidCheck i;
		synchronized (inProgress) {
			i = inProgress.get(versionId);
		}
		if (i != null)
			return i.result;
		synchronized (done) {
			i = done.get(versionId);
		}
		if (i != null)
			return i.result;
		return CheckResult.NO_SUCH_JOB;
	}

	/**
	 * Get status/result of all known check jobs.
	 * 
	 * @return MAP
	 */
	public static Map<String, CheckResult> getAll() {
		Map<String, CheckResult> res = new HashMap<>();
		synchronized (inProgress) {
			for (Entry<String, ImageValidCheck> i : inProgress.entrySet()) {
				res.put(i.getKey(), i.getValue().result);
			}
		}
		synchronized (done) {
			for (Entry<String, ImageValidCheck> i : done.getImmutableSnapshot().entrySet()) {
				res.put(i.getKey(), i.getValue().result);
			}
		}
		return res;
	}

	//
	// Instance
	//

	private ImageValidCheck(String versionId, boolean integrity, boolean updateState) {
		this.versionId = versionId;
		this.integrity = integrity;
		this.updateState = updateState;
	}

	private void setState(CheckResult cr) {
		if (cr.stage > result.stage) {
			result = cr;
		} else {
			LOGGER.debug("Ingoring state update from " + result.name() + " to " + cr.name());
		}
	}

	@Override
	public void run() {
		try {
			setState(CheckResult.WAITING_FOR_STORE);
			if (!FileSystem.waitForStorage()) {
				LOGGER.warn("Will not check " + versionId + ": Storage not online");
				setState(CheckResult.INTERNAL_ERROR);
				return;
			}
			setState(CheckResult.WORKING);
			LocalImageVersion imageVersion;
			try {
				imageVersion = DbImage.getLocalImageData(versionId);
			} catch (TNotFoundException e) {
				LOGGER.warn("Cannot check validity of image version - not found: " + versionId);
				setState(CheckResult.UNKNOWN_VERSIONID);
				return;
			} catch (Exception e) {
				LOGGER.warn("Cannot get local image data", e);
				setState(CheckResult.INTERNAL_ERROR);
				return;
			}
			// Found image in DB
			// Simple checks first
			boolean valid = checkValid(imageVersion);
			if (valid && integrity) {
				// Check block hashes
				try {
					valid = checkBlockHashes(imageVersion);
				} catch (IOException e) {
					LOGGER.warn("IO error for " + versionId, e);
					setState(CheckResult.FILE_ACCESS_ERROR);
					valid = false;
				} catch (Exception e) {
					LOGGER.warn("Cannot check block hashes of " + versionId, e);
					setState(CheckResult.INTERNAL_ERROR);
				}
			}
			if (imageVersion.isValid == valid) {
				// nothing changed
				if (valid) {
					setState(CheckResult.DONE);
				}
				return;
			}
			// Update
			try {
				if (updateState) {
					DbImage.markValid(valid, false, imageVersion);
				}
				if (valid) {
					setState(CheckResult.DONE);
				}
			} catch (SQLException e) {
				setState(CheckResult.INTERNAL_ERROR);
			}
		} finally {
			if (result == CheckResult.WORKING) {
				setState(CheckResult.INTERNAL_ERROR);
			}
			ImageValidCheck ivc;
			synchronized (inProgress) {
				synchronized (done) {
					ivc = inProgress.remove(this.versionId);
					done.put(this.versionId, ivc);
				}
			}
			checkForWork();
		}
	}

	/**
	 * Do a complete hash check of the given image file.
	 */
	private boolean checkBlockHashes(final LocalImageVersion imageVersion) throws IOException,
			InterruptedException {
		ImageVersionMeta versionDetails;
		try {
			versionDetails = DbImage.getVersionDetails(versionId);
		} catch (TNotFoundException e) {
			LOGGER.warn("Cannot check hash of image version - not found: " + versionId);
			setState(CheckResult.UNKNOWN_VERSIONID);
			return false;
		} catch (SQLException e) {
			setState(CheckResult.INTERNAL_ERROR);
			return false;
		}
		if (versionDetails.sha1sums == null || versionDetails.sha1sums.isEmpty()) {
			LOGGER.info("Image does not have block hashes -- assuming ok");
			return true;
		}
		int numChecked = 0;
		final Semaphore sem = new Semaphore(0);
		final AtomicBoolean fileOk = new AtomicBoolean(true);
		File path = FileSystem.composeAbsoluteImagePath(imageVersion);
		try (RandomAccessFile raf = new RandomAccessFile(path, "r")) {
			long startOffset = 0;
			for (ByteBuffer hash : versionDetails.sha1sums) {
				if (hash == null) {
					startOffset += FileChunk.CHUNK_SIZE;
					continue;
				}
				long endOffset = startOffset + FileChunk.CHUNK_SIZE;
				if (endOffset > imageVersion.fileSize) {
					endOffset = imageVersion.fileSize;
				}
				StandaloneFileChunk chunk = new StandaloneFileChunk(startOffset, endOffset,
						ThriftUtil.unwrapByteBuffer(hash));
				byte[] buffer = new byte[(int) (endOffset - startOffset)];
				raf.seek(startOffset);
				raf.readFully(buffer);
				hashChecker.queue(chunk, buffer, new HashCheckCallback() {
					@Override
					public void hashCheckDone(HashResult result, byte[] data, FileChunk chunk) {
						try {
							if (result == HashResult.FAILURE) {
								// Hashing failed, cannot tell whether OK or not :(
							} else {
								if (result == HashResult.INVALID) {
									fileOk.set(false);
									((StandaloneFileChunk) chunk).overrideStatus(ChunkStatus.MISSING);
								} else {
									// >:(
									((StandaloneFileChunk) chunk).overrideStatus(ChunkStatus.COMPLETE);
								}
								try {
									// We don't know what the state was in DB before, so just fire updates
									DbImageBlock.asyncUpdate(imageVersion.imageVersionId, chunk);
								} catch (InterruptedException e) {
									Thread.currentThread().interrupt();
								}
							}
						} finally {
							sem.release();
						}
					}
				}, HashChecker.BLOCKING | HashChecker.CALC_HASH);
				numChecked += 1;
				startOffset += FileChunk.CHUNK_SIZE;
			}
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
			throw e;
		}
		// Wait until the last callback fired
		sem.acquire(numChecked);
		if (fileOk.get()) {
			return true;
		}
		setState(CheckResult.FILE_CORRUPT);
		return false;
	}

	/**
	 * "Inexpensive" validity checks. File exists, readable, size ok, etc.
	 */
	private boolean checkValid(LocalImageVersion imageVersion) {
		if (imageVersion == null)
			return false;
		if (imageVersion.expireTime < Util.unixTime()) {
			LOGGER.info(versionId + ": expired");
			setState(CheckResult.VERSION_EXPIRED);
			return false;
		}
		if (imageVersion.filePath == null || imageVersion.filePath.isEmpty()) {
			LOGGER.info(versionId + ": DB does not contain a path");
			setState(CheckResult.DATABASE_PATH_MISSING);
			return false;
		}
		File path = FileSystem.composeAbsoluteImagePath(imageVersion);
		if (path == null) {
			LOGGER.info(versionId + ": path from DB is not valid");
			setState(CheckResult.DATABASE_PATH_INVALID);
			return false;
		}
		if (!path.exists()) {
			LOGGER.info(versionId + ": File does not exist (" + path.getAbsolutePath() + ")");
			setState(CheckResult.FILE_NOT_FOUND);
			return false;
		}
		if (!path.canRead()) {
			LOGGER.info(versionId + ": File exists but not readable (" + path.getAbsolutePath() + ")");
			setState(CheckResult.FILE_ACCESS_ERROR);
			return false;
		}
		if (path.length() != imageVersion.fileSize) {
			LOGGER.info(versionId + ": File exists but has wrong size (expected: " + imageVersion.fileSize
					+ ", found: " + path.length() + ")");
			setState(CheckResult.FILE_SIZE_MISMATCH);
			return false;
		}
		return true;
	}

}