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
|
package org.openecomp.core.tools.commands;
import org.apache.commons.collections.CollectionUtils;
import org.openecomp.core.tools.concurrent.ItemHealingTask;
import org.openecomp.core.tools.exceptions.HealingRuntimeException;
import org.openecomp.core.tools.loaders.VersionInfoCassandraLoader;
import org.openecomp.sdc.healing.api.HealingManager;
import org.openecomp.sdc.healing.factory.HealingManagerFactory;
import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Stream;
/**
* Created by ayalaben on 11/6/2017
*/
public class HealAll {
private static final int DEFAULT_THREAD_NUMBER = 100;
private static List<ItemHealingTask> tasks = new ArrayList<>();
private static VendorSoftwareProductManager vspManager = VspManagerFactory
.getInstance().createInterface();
private static HealingManager healingManager = HealingManagerFactory.getInstance()
.createInterface();
private HealAll() {
}
public static void healAll(String threadNumber) {
String logFileName = "healing.log";
try (BufferedWriter log = new BufferedWriter(new FileWriter(logFileName, true))) {
writeToLog("----starting healing------", log);
Instant startTime = Instant.now();
int numberOfThreads = Objects.nonNull(threadNumber) ? Integer.valueOf(threadNumber) :
DEFAULT_THREAD_NUMBER;
ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
filterByEntityType(VersionInfoCassandraLoader.list(),
VendorSoftwareProductConstants.VENDOR_SOFTWARE_PRODUCT_VERSIONABLE_TYPE).forEach
(HealAll::addTaskToTasks);
executeAllTasks(executor, log);
writeToLog("----finished healing------", log);
Instant endTime = Instant.now();
writeToLog("Total runtime was: " + Duration.between(startTime, endTime), log);
} catch (IOException e) {
throw new HealingRuntimeException("can't initial healing log file '" + logFileName + "'", e);
}
System.exit(1);
}
private static void executeAllTasks(ExecutorService executor, BufferedWriter log) {
List<Future<String>> futureTasks;
try {
futureTasks = executor.invokeAll(tasks);
futureTasks.forEach(future -> {
try {
log.write(future.get());
log.newLine();
} catch (Exception e) {
writeToLog(e.getMessage(), log);
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
writeToLog("migration tasks failed with message: " + e.getMessage(), log);
throw new HealingRuntimeException(e);
}
boolean isThreadOpen = true;
while (isThreadOpen) {
isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone());
}
}
private static Version resolveVersion(VersionInfoEntity versionInfoEntity) {
if (Objects.nonNull(versionInfoEntity.getCandidate())) {
return versionInfoEntity.getCandidate().getVersion();
} else if (!CollectionUtils.isEmpty(versionInfoEntity.getViewableVersions())) {
return versionInfoEntity.getViewableVersions().stream().max(Version::compateTo)
.orElse(new Version());
}
return versionInfoEntity.getActiveVersion();
}
private static void writeToLog(String message, BufferedWriter log) {
try {
log.write(message);
log.newLine();
} catch (IOException e) {
throw new HealingRuntimeException("unable to write to healing all log file.", e);
}
}
private static Stream<VersionInfoEntity> filterByEntityType(
Collection<VersionInfoEntity> versionInfoEntities, String entityType) {
return versionInfoEntities.stream().filter(versionInfoEntity -> versionInfoEntity
.getEntityType().equals(entityType));
}
private static void addTaskToTasks(VersionInfoEntity versionInfoEntity) {
tasks.add(new ItemHealingTask(versionInfoEntity.getEntityId(), resolveVersion
(versionInfoEntity).toString(),
vspManager, healingManager));
}
}
|