-
Notifications
You must be signed in to change notification settings - Fork 7
/
MoveIndexAliasJobFactory.java
120 lines (102 loc) · 4.24 KB
/
MoveIndexAliasJobFactory.java
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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.biz.elastic;
import sirius.biz.jobs.StandardCategories;
import sirius.biz.jobs.batch.SimpleBatchProcessJobFactory;
import sirius.biz.jobs.params.EntityDescriptorParameter;
import sirius.biz.jobs.params.Parameter;
import sirius.biz.jobs.params.StringParameter;
import sirius.biz.process.ProcessContext;
import sirius.biz.process.Processes;
import sirius.biz.tenants.TenantUserManager;
import sirius.db.es.Elastic;
import sirius.db.es.IndexMappings;
import sirius.db.mixing.EntityDescriptor;
import sirius.db.mixing.Mixing;
import sirius.kernel.commons.Json;
import sirius.kernel.commons.Strings;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.HandledException;
import sirius.web.security.Permission;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Map;
import java.util.function.Consumer;
/**
* Implements a job which moves the alias which marks an active index to a desired destination index.
*/
@Register(framework = Processes.FRAMEWORK_PROCESSES)
@Permission(TenantUserManager.PERMISSION_SYSTEM_ADMINISTRATOR)
public class MoveIndexAliasJobFactory extends SimpleBatchProcessJobFactory {
@Part
private Elastic elastic;
@Part
private Mixing mixing;
@Part
private IndexMappings mappings;
private final Parameter<EntityDescriptor> entityDescriptorParameter =
new EntityDescriptorParameter().withFilter(EntityDescriptorParameter::isElasticEntity)
.markRequired()
.build();
private final Parameter<String> destinationParameter =
new StringParameter("destination", "Destination").markRequired().build();
@Override
public String getLabel() {
return "Move Elasticsearch Index Alias";
}
@Nullable
@Override
public String getDescription() {
return "Moves the alias of the given entity to the given target index.";
}
@Override
protected String createProcessTitle(Map<String, String> context) {
try {
return Strings.apply("Moving active Elasticsearch alias from index '%s' to '%s'",
elastic.determineEffectiveIndex(entityDescriptorParameter.require(context)),
destinationParameter.require(context));
} catch (HandledException exception) {
// In some rare cases, this might fail (if the system is inconsistent anyway). In this case
// we prefer that the job fails rather than the setup / start crashes...
Exceptions.ignore(exception);
return Strings.apply("Moving active Elasticsearch alias to '%s'", destinationParameter.require(context));
}
}
@Override
protected void execute(ProcessContext process) throws Exception {
String destination = process.require(destinationParameter);
EntityDescriptor ed = process.require(entityDescriptorParameter);
process.log(Json.write(elastic.getLowLevelClient()
.createOrMoveAlias(elastic.determineReadAlias(ed), destination)));
String effectiveIndex = elastic.determineEffectiveIndex(ed);
process.log(Strings.apply("Setting dynamic mapping mode to 'strict' for index '%s'.", effectiveIndex));
// Set the dynamic mapping mode to 'strict' as most probably it is currently set to 'false' which was used
// during reindexing (see ReindexJobFactory)
mappings.createMapping(ed, effectiveIndex, IndexMappings.DynamicMapping.STRICT);
}
@Override
protected void collectParameters(Consumer<Parameter<?>> parameterCollector) {
parameterCollector.accept(entityDescriptorParameter);
parameterCollector.accept(destinationParameter);
}
@Nonnull
@Override
public String getName() {
return "move-index-alias";
}
@Override
public int getPriority() {
return 10210;
}
@Override
public String getCategory() {
return StandardCategories.SYSTEM_ADMINISTRATION;
}
}