-
Notifications
You must be signed in to change notification settings - Fork 7
/
FixCorruptVersions-OAK-5193.groovy
275 lines (240 loc) · 11.8 KB
/
FixCorruptVersions-OAK-5193.groovy
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
import com.google.common.collect.BiMap
import com.google.common.collect.HashBiMap
import com.google.common.collect.Maps
import com.google.common.collect.Sets
import groovy.transform.EqualsAndHashCode
import org.apache.commons.lang3.StringUtils
import org.apache.jackrabbit.oak.api.CommitFailedException
import org.apache.jackrabbit.oak.api.PropertyState
import org.apache.jackrabbit.oak.api.Type
import org.apache.jackrabbit.oak.plugins.nodetype.TypePredicate
import org.apache.jackrabbit.oak.spi.commit.CommitInfo
import org.apache.jackrabbit.oak.spi.commit.DefaultEditor
import org.apache.jackrabbit.oak.spi.commit.Editor
import org.apache.jackrabbit.oak.spi.commit.EditorProvider
import org.apache.jackrabbit.oak.spi.commit.EmptyHook
import org.apache.jackrabbit.oak.spi.state.ChildNodeEntry
import org.apache.jackrabbit.oak.spi.state.NodeBuilder
import org.apache.jackrabbit.oak.spi.state.NodeState
import org.apache.jackrabbit.oak.spi.state.NodeStore
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.ArrayList
import java.util.HashSet
import java.util.List
import java.util.Set
import static org.apache.jackrabbit.JcrConstants.JCR_BASEVERSION
import static org.apache.jackrabbit.JcrConstants.JCR_FROZENMIXINTYPES
import static org.apache.jackrabbit.JcrConstants.JCR_FROZENNODE
import static org.apache.jackrabbit.JcrConstants.JCR_ISCHECKEDOUT
import static org.apache.jackrabbit.JcrConstants.JCR_MIXINTYPES
import static org.apache.jackrabbit.JcrConstants.JCR_PREDECESSORS
import static org.apache.jackrabbit.JcrConstants.JCR_ROOTVERSION
import static org.apache.jackrabbit.JcrConstants.JCR_SUCCESSORS
import static org.apache.jackrabbit.JcrConstants.JCR_SYSTEM
import static org.apache.jackrabbit.JcrConstants.JCR_UUID
import static org.apache.jackrabbit.JcrConstants.JCR_VERSIONHISTORY
import static org.apache.jackrabbit.JcrConstants.JCR_VERSIONSTORAGE
import static org.apache.jackrabbit.JcrConstants.MIX_VERSIONABLE
import static org.apache.jackrabbit.JcrConstants.NT_FROZENNODE
import static org.apache.jackrabbit.JcrConstants.NT_VERSION
import static org.apache.jackrabbit.JcrConstants.NT_VERSIONHISTORY
import static org.apache.jackrabbit.oak.api.Type.NAMES
import static org.apache.jackrabbit.oak.api.Type.REFERENCE
import static org.apache.jackrabbit.oak.api.Type.REFERENCES
import static org.apache.jackrabbit.oak.plugins.memory.MultiGenericPropertyState.nameProperty
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* This script removes invalid successor and predecessor references which may be
* result of the OAK-5193 bug (https://issues.apache.org/jira/browse/OAK-5193).
* It should be run with the oak-run:
*
* <pre>
* java -jar oak-run-*.jar console repository/segmentstore ":load OAK-5193-fix.groovy" | tee script.log
* </pre>
*/
class Oak5193Fixer {
final Logger log = LoggerFactory.getLogger(getClass())
private final NodeStore ns
private final NodeBuilder rootBuilder
private final TypePredicate isVersionHistory
private final TypePredicate isVersion
private Oak5193Fixer(NodeBuilder rootBuilder, NodeStore ns) {
this.rootBuilder = rootBuilder
this.ns = ns
this.isVersionHistory = new TypePredicate(rootBuilder.getNodeState(), NT_VERSIONHISTORY)
this.isVersion = new TypePredicate(rootBuilder.getNodeState(), NT_VERSION)
}
private void traverse() {
traverse(rootBuilder.getChildNode(JCR_SYSTEM).getChildNode(JCR_VERSIONSTORAGE))
ns.merge(rootBuilder, EmptyHook.INSTANCE, CommitInfo.EMPTY)
doLog("Merged fixes")
}
private void traverse(NodeBuilder builder) {
for (String childName : builder.getChildNodeNames()) {
NodeBuilder child = builder.getChildNode(childName)
if (isVersionHistory.apply(child.getNodeState())) {
fixVersionHistory(child)
} else {
traverse(child)
}
}
}
private boolean fixVersionHistory(NodeBuilder versionHistory) {
log.debug("Validating " + versionHistory)
BiMap<String, String> uuidName = HashBiMap.create()
for (String childName : versionHistory.getChildNodeNames()) {
NodeBuilder child = versionHistory.getChildNode(childName)
if (isVersion.apply(child.getNodeState())) {
String uuid = child.getProperty(JCR_UUID).getValue(Type.STRING)
uuidName.put(uuid, childName)
}
}
for (String versionName : uuidName.values()) {
NodeBuilder version = versionHistory.getChildNode(versionName)
Set<String> predecessors = Sets.newLinkedHashSet((Iterable<String>) version.getProperty(JCR_PREDECESSORS).getValue(Type.REFERENCES))
Set<String> invalidPredecessors = Sets.difference(predecessors, uuidName.keySet())
for (String invalidPredecessorUuid : invalidPredecessors) {
String validPredecessor = fixInvalidPredecessor(versionHistory, uuidName, invalidPredecessorUuid, versionName, version)
doLog(versionHistory.toString() + " (!) fixed predecessor " + versionName + " <- " + validPredecessor)
}
for (String invalidSuccessorUuid : intersectReferences(version, JCR_SUCCESSORS, uuidName.keySet())) {
doLog(versionHistory.toString() + " (!) removed invalid successors " + versionName + " -> " + invalidSuccessorUuid)
}
}
for (String versionName : uuidName.values()) {
NodeBuilder version = versionHistory.getChildNode(versionName)
for (String predecessor : setMissingSuccessors(versionHistory, uuidName, version)) {
doLog(versionHistory.toString() + " (!) created missing successor " + predecessor + " -> " + versionName)
}
for (String successor : removeRedundantSuccessors(versionHistory, uuidName, version)) {
doLog(versionHistory.toString() + " (!) removed redundant successor " + versionName + " -> " + successor)
}
}
}
private String fixInvalidPredecessor(NodeBuilder versionHistory, BiMap<String, String> uuidName, String invalidPredecessorUuid, String versionName, NodeBuilder version) {
String validPredecessorName = closestVersion(versionName, uuidName.values())
Set<String> predecessors = Sets.newLinkedHashSet(version.getProperty(JCR_PREDECESSORS).getValue(Type.REFERENCES))
predecessors.remove(invalidPredecessorUuid)
if (validPredecessorName != null) {
predecessors.add(uuidName.inverse().get(validPredecessorName))
}
version.setProperty(JCR_PREDECESSORS, predecessors, Type.REFERENCES)
return validPredecessorName
}
private Set<String> intersectReferences(NodeBuilder builder, String propertyName, Set<String> references) {
Set<String> currentValue = Sets.newLinkedHashSet((Iterable<String>) builder.getProperty(propertyName).getValue(Type.REFERENCES))
Set<String> intersection = Sets.intersection(currentValue, references)
if (currentValue != intersection) {
builder.setProperty(propertyName, intersection, Type.REFERENCES)
return Sets.difference(currentValue, references)
} else {
return Collections.emptySet()
}
}
private static List<String> setMissingSuccessors(NodeBuilder versionHistory, BiMap<String, String> uuidName, NodeBuilder version) {
String uuid = version.getProperty(JCR_UUID).getValue(Type.STRING)
Set<String> predecessors = Sets.newLinkedHashSet((Iterable<String>) version.getProperty(JCR_PREDECESSORS).getValue(Type.REFERENCES))
List<String> fixedPredecessors = new ArrayList<>()
for (String pUuid : predecessors) {
String pName = uuidName.get(pUuid)
NodeBuilder predecessor = versionHistory.getChildNode(pName)
Set<String> successors = Sets.newLinkedHashSet((Iterable<String>) predecessor.getProperty(JCR_SUCCESSORS).getValue(Type.REFERENCES))
if (!successors.contains(uuid)) {
successors.add(uuid)
predecessor.setProperty(JCR_SUCCESSORS, successors, Type.REFERENCES)
fixedPredecessors.add(pName)
}
}
return fixedPredecessors
}
private static List<String> removeRedundantSuccessors(NodeBuilder versionHistory, BiMap<String, String> uuidName, NodeBuilder version) {
String uuid = version.getProperty(JCR_UUID).getValue(Type.STRING)
Set<String> successors = Sets.newLinkedHashSet((Iterable<String>) version.getProperty(JCR_SUCCESSORS).getValue(Type.REFERENCES))
List<String> removedSuccessors = new ArrayList<>()
Iterator<String> successorsIt = successors.iterator()
while (successorsIt.hasNext()) {
String sUuid = successorsIt.next()
String sName = uuidName.get(sUuid)
NodeBuilder successor = versionHistory.getChildNode(sName)
Set<String> predecessors = Sets.newLinkedHashSet((Iterable<String>) successor.getProperty(JCR_PREDECESSORS).getValue(Type.REFERENCES))
if (!predecessors.contains(uuid)) {
removedSuccessors.add(sName)
successorsIt.remove()
}
}
if (!removedSuccessors.isEmpty()) {
version.setProperty(JCR_SUCCESSORS, successors, Type.REFERENCES)
}
return removedSuccessors
}
static String closestVersion(String version, Set<String> versions) {
String v = version
while (true) {
v = decreaseVersion(v)
if (v == JCR_ROOTVERSION || versions.contains(v)) {
return v
}
}
}
static String decreaseVersion(String version) {
if (version == JCR_ROOTVERSION) {
return null
} else if (version == "1.0") {
return JCR_ROOTVERSION
}
String[] split = StringUtils.split(version, '.')
String[] result
int lastSegment = Integer.valueOf(split[split.length - 1])
if (lastSegment == 0) {
result = new String[split.length - 1]
for (int i = 0; i < result.length; i++) {
result[i] = split[i]
}
} else {
result = split
result[split.length - 1] = String.valueOf(lastSegment - 1)
}
return StringUtils.join(result, '.')
}
private void doLog(String message) {
log.info(message);
}
}
def test() {
def decSeq = { String version ->
List<String> seq = new ArrayList<>()
String v = version
while (v != null) {
seq.add(v)
v = Oak5193Fixer.decreaseVersion(v)
}
return seq
}
assert decSeq("1.2.1.6") == ["1.2.1.6", "1.2.1.5", "1.2.1.4", "1.2.1.3", "1.2.1.2", "1.2.1.1", "1.2.1.0", "1.2.1", "1.2.0", "1.2", "1.1", "1.0", "jcr:rootVersion"]
assert decSeq("1.0") == ["1.0", "jcr:rootVersion"]
assert decSeq("jcr:rootVersion") == ["jcr:rootVersion"]
assert Oak5193Fixer.closest("1.2.1.6", ["1.3", "1.2.1.6", "1.2.1", "1.2", "jcr:rootVersion"]) == "1.2.1"
assert Oak5193Fixer.closest("1.2.1.6", ["1.3", "1.2.1.5", "1.2.1", "1.2", "jcr:rootVersion"]) == "1.2.1.5"
assert Oak5193Fixer.closest("1.3", ["1.3", "1.1", "jcr:rootVersion"]) == "1.1"
assert Oak5193Fixer.closest("1.3", ["1.3", "jcr:rootVersion"]) == "jcr:rootVersion"
assert Oak5193Fixer.closest("1.0", ["1.0", "jcr:rootVersion"]) == "jcr:rootVersion"
assert Oak5193Fixer.closest("jcr:rootVersion", ["jcr:rootVersion"]) == null
}
def runFixer(session) {
NodeStore ns = session.getRootNode().sessionDelegate.root.store
def rootBuilder = ns.root.builder()
new Oak5193Fixer(rootBuilder, ns).traverse()
null
}
def fixVersions() {
def repo = osgi.getService(org.apache.sling.jcr.api.SlingRepository)
def session = repo.loginAdministrative(null)
try {
runFixer(session)
} finally {
session.logout()
}
}
fixVersions()