forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseVersionHierarchy.java
More file actions
199 lines (171 loc) · 7.96 KB
/
DatabaseVersionHierarchy.java
File metadata and controls
199 lines (171 loc) · 7.96 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.upgrade;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableList;
import org.apache.cloudstack.utils.CloudStackVersion;
import com.cloud.upgrade.dao.DbUpgrade;
/**
* @since 4.12.0.0
*/
public final class DatabaseVersionHierarchy {
private final ImmutableList<VersionNode> hierarchy;
private DatabaseVersionHierarchy(ImmutableList<VersionNode> hierarchy) {
this.hierarchy = hierarchy;
}
public static DatabaseVersionHierarchyBuilder builder() {
return new DatabaseVersionHierarchyBuilder();
}
/**
* Check if current hierarchy of Database Versions contains <code>version</code>.
*
* @param version The version to check if hierarchy contains it
*
* @return true if hierarchy contains the version, false if not
*/
public boolean contains(final CloudStackVersion version) {
return toList().contains(version);
}
/**
* Calculates an upgrade path for the passed <code>fromVersion</code>. If the <code>fromVersion</code>
* doesn't exist in list of available <code>VersionNode</code> hierarchy, then calculation assumes that
* the <code>fromVersion</code> required no schema migrations or data conversions and no upgrade path was
* defined for it. Therefore, we find the most recent version with database migrations before the
* <code>fromVersion</code> and adopt that upgrade path list.
*
* @param fromVersion The version from which the upgrade will occur
*
* @return The upgrade path from <code>fromVersion</code> to <code>LATEST</code> version.
*/
public DbUpgrade[] getPath(final CloudStackVersion fromVersion) {
return getPath(fromVersion, null);
}
/**
* Calculates an upgrade path for the passed <code>fromVersion</code> and <code>toVersion</code>. If the
* <code>fromVersion</code> doesn't exist in list of available <code>VersionNode</code> hierarchy, then
* calculation assumes that the <code>fromVersion</code> required no schema migrations or data conversions
* and no upgrade path was defined for it. Therefore, we find the most recent version with database
* migrations before the <code>fromVersion</code> and adopt that upgrade path list up to <code>toVersion</code>.
* If <code>toVersion</code> is null, we're going to find the upgrade path up to the latest available version.
*
* @param fromVersion The version from which the upgrade will occur
* @param toVersion The version up to which the upgrade will occur (can be null)
*
* @return The upgrade path from <code>fromVersion</code> to <code>toVersion</code>
*/
public DbUpgrade[] getPath(final CloudStackVersion fromVersion, final CloudStackVersion toVersion) {
if (fromVersion == null) {
return new DbUpgrade[0];
}
// The CloudStack version is latest or higher than latest
if (fromVersion.compareTo(getLatestVersion()) >= 0) {
return new DbUpgrade[0];
}
// we cannot find the version specified, so get the
// most recent one immediately before this version
if (!contains(fromVersion)) {
DbUpgrade[] dbUpgrades = getPath(getRecentVersion(fromVersion), toVersion);
return Arrays.stream(dbUpgrades).filter(up -> CloudStackVersion.compare(up.getUpgradedVersion(), fromVersion.toString()) > 0)
.toArray(DbUpgrade[]::new);
}
final Predicate<? super VersionNode> predicate;
if (toVersion == null) {
// all the available versions greater than or equal to fromVersion
predicate = node -> node.version.compareTo(fromVersion) > -1;
} else {
// all the available versions greater than or equal to fromVersion AND less than toVersion
predicate = node -> node.version.compareTo(fromVersion) > -1 && node.version.compareTo(toVersion) < 0;
}
// get upgrade path from version forward (include version itself in the path)
return hierarchy
.stream()
.filter(predicate)
.filter(distinct(node -> node.upgrader.getUpgradedVersion()))
.map(node -> node.upgrader)
.toArray(DbUpgrade[]::new);
}
/**
* Find the most recent <code>CloudStackVersion</code> immediately before <code>fromVersion</code>
*
* @param fromVersion The version to look up its immediate previous available version
*
* @return The <code>CloudStackVersion</code> or null
*
* @since 4.8.2.0 (refactored in 4.11.1.0)
*/
protected CloudStackVersion getRecentVersion(final CloudStackVersion fromVersion) {
if (fromVersion == null) {
return null;
}
// find the most recent version immediately before fromVersion
return toList()
.reverse()
.stream()
.filter(version -> fromVersion.compareTo(version) > 0)
.findFirst()
.orElse(null);
}
/**
* Generate immutable list of available <code>CloudstackVersion</code> in the hierarchy
*
* @return list of available versions
*/
public ImmutableList<CloudStackVersion> toList() {
List<CloudStackVersion> versions = hierarchy
.stream()
.map(node -> node.version)
.collect(Collectors.toList());
return ImmutableList.copyOf(versions);
}
/**
* Find the distinct <code>VersionNode</code> based on the provided <code>getUpgradedVersion()</code>
*/
private Predicate<VersionNode> distinct(Function<VersionNode, String> keyExtractor) {
Map<String, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
protected static class VersionNode {
final CloudStackVersion version;
final DbUpgrade upgrader;
protected VersionNode(final CloudStackVersion version, final DbUpgrade upgrader) {
this.version = version;
this.upgrader = upgrader;
}
}
public static final class DatabaseVersionHierarchyBuilder {
private final List<VersionNode> hierarchyBuilder = new LinkedList<>();
private DatabaseVersionHierarchyBuilder() {
}
public DatabaseVersionHierarchyBuilder next(final String version, final DbUpgrade upgrader) {
hierarchyBuilder.add(new VersionNode(CloudStackVersion.parse(version), upgrader));
return this;
}
public DatabaseVersionHierarchy build() {
return new DatabaseVersionHierarchy(ImmutableList.copyOf(hierarchyBuilder));
}
}
public CloudStackVersion getLatestVersion() {
return CloudStackVersion.parse(hierarchy.get(hierarchy.size() - 1).upgrader.getUpgradedVersion());
}
}