-
Notifications
You must be signed in to change notification settings - Fork 474
/
TestOMDirectoriesPurgeRequestAndResponse.java
228 lines (200 loc) · 9.26 KB
/
TestOMDirectoriesPurgeRequestAndResponse.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
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
/**
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 org.apache.hadoop.ozone.om.request.key;
import com.google.common.base.Optional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.hadoop.hdds.client.BlockID;
import org.apache.hadoop.hdds.utils.db.BatchOperation;
import org.apache.hadoop.hdds.utils.db.cache.CacheKey;
import org.apache.hadoop.hdds.utils.db.cache.CacheValue;
import org.apache.hadoop.ozone.ClientVersion;
import org.apache.hadoop.ozone.om.helpers.BucketLayout;
import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo;
import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup;
import org.apache.hadoop.ozone.om.request.OMRequestTestUtils;
import org.apache.hadoop.ozone.om.response.key.OMDirectoriesPurgeResponseWithFSO;
import org.apache.hadoop.ozone.om.response.key.OMKeyPurgeResponse;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos;
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest;
import org.junit.Assert;
import org.junit.Test;
/**
* Tests {@link OMKeyPurgeRequest} and {@link OMKeyPurgeResponse}.
*/
public class TestOMDirectoriesPurgeRequestAndResponse extends TestOMKeyRequest {
private int numKeys = 10;
/**
* Creates volume, bucket and key entries and adds to OM DB and then
* deletes these keys to move them to deletedKeys table.
*/
private List<OmKeyInfo> createAndDeleteKeys(Integer trxnIndex, String bucket)
throws Exception {
if (bucket == null) {
bucket = bucketName;
}
// Add volume, bucket and key entries to OM DB.
OMRequestTestUtils.addVolumeAndBucketToDB(volumeName, bucket,
omMetadataManager);
List<OmKeyInfo> deletedKeyNames = new ArrayList<>(numKeys);
List<String> ozoneKeyNames = new ArrayList<>(numKeys);
for (int i = 1; i <= numKeys; i++) {
String key = keyName + "-" + i;
OMRequestTestUtils.addKeyToTable(false, false, volumeName, bucket,
key, clientID, replicationType, replicationFactor, trxnIndex++,
omMetadataManager);
String ozoneKey = omMetadataManager.getOzoneKey(
volumeName, bucket, key);
ozoneKeyNames.add(ozoneKey);
OmKeyInfo omKeyInfo =
omMetadataManager.getKeyTable(BucketLayout.DEFAULT).get(ozoneKey);
deletedKeyNames.add(omKeyInfo);
updateBlockInfo(omKeyInfo);
}
for (String ozoneKey : ozoneKeyNames) {
OMRequestTestUtils.deleteKey(
ozoneKey, omMetadataManager, trxnIndex++);
}
return deletedKeyNames;
}
private void updateBlockInfo(OmKeyInfo omKeyInfo) throws IOException {
String bucketKey = omMetadataManager.getBucketKey(volumeName, bucketName);
OmBucketInfo omBucketInfo = omMetadataManager.getBucketTable().get(
bucketKey);
List<OmKeyLocationInfoGroup> locationList = new ArrayList<>();
List<OmKeyLocationInfo> locList = new ArrayList<>();
OmKeyLocationInfo.Builder builder = new OmKeyLocationInfo.Builder();
builder.setLength(omKeyInfo.getDataSize()).setBlockID(new BlockID(1, 1));
locList.add(builder.build());
locationList.add(new OmKeyLocationInfoGroup(1, locList, false));
omKeyInfo.setKeyLocationVersions(locationList);
omBucketInfo.incrUsedBytes(omKeyInfo.getDataSize());
omBucketInfo.incrUsedNamespace(1L);
omMetadataManager.getBucketTable().addCacheEntry(new CacheKey<>(bucketKey),
new CacheValue<>(Optional.of(omBucketInfo), 1L));
omMetadataManager.getBucketTable().put(bucketKey, omBucketInfo);
}
/**
* Create OMRequest which encapsulates DeleteKeyRequest.
* @return OMRequest
*/
private OMRequest createPurgeKeysRequest(String purgeDeletedDir,
List<OmKeyInfo> keyList) throws IOException {
List<OzoneManagerProtocolProtos.PurgePathRequest> purgePathRequestList
= new ArrayList<>();
List<OmKeyInfo> subFiles = new ArrayList<>();
for (OmKeyInfo key : keyList) {
subFiles.add(key);
}
List<OmKeyInfo> subDirs = new ArrayList<>();
Long volumeId = 1L;
Long bucketId = 1L;
OzoneManagerProtocolProtos.PurgePathRequest request = wrapPurgeRequest(
volumeId, bucketId, purgeDeletedDir, subFiles, subDirs);
purgePathRequestList.add(request);
OzoneManagerProtocolProtos.PurgeDirectoriesRequest.Builder purgeDirRequest =
OzoneManagerProtocolProtos.PurgeDirectoriesRequest.newBuilder();
purgeDirRequest.addAllDeletedPath(purgePathRequestList);
OzoneManagerProtocolProtos.OMRequest omRequest =
OzoneManagerProtocolProtos.OMRequest.newBuilder()
.setCmdType(OzoneManagerProtocolProtos.Type.PurgeDirectories)
.setPurgeDirectoriesRequest(purgeDirRequest)
.setClientId(UUID.randomUUID().toString())
.build();
return omRequest;
}
private OzoneManagerProtocolProtos.PurgePathRequest wrapPurgeRequest(
final long volumeId, final long bucketId, final String purgeDeletedDir,
final List<OmKeyInfo> purgeDeletedFiles,
final List<OmKeyInfo> markDirsAsDeleted) {
// Put all keys to be purged in a list
OzoneManagerProtocolProtos.PurgePathRequest.Builder purgePathsRequest
= OzoneManagerProtocolProtos.PurgePathRequest.newBuilder();
purgePathsRequest.setVolumeId(volumeId);
purgePathsRequest.setBucketId(bucketId);
if (purgeDeletedDir != null) {
purgePathsRequest.setDeletedDir(purgeDeletedDir);
}
for (OmKeyInfo purgeFile : purgeDeletedFiles) {
purgePathsRequest.addDeletedSubFiles(
purgeFile.getProtobuf(true, ClientVersion.CURRENT_VERSION));
}
// Add these directories to deletedDirTable, so that its sub-paths will be
// traversed in next iteration to ensure cleanup all sub-children.
for (OmKeyInfo dir : markDirsAsDeleted) {
purgePathsRequest.addMarkDeletedSubDirs(
dir.getProtobuf(ClientVersion.CURRENT_VERSION));
}
return purgePathsRequest.build();
}
private OMRequest preExecute(OMRequest originalOmRequest) throws IOException {
OMKeyPurgeRequest omKeyPurgeRequest =
new OMKeyPurgeRequest(originalOmRequest);
OMRequest modifiedOmRequest = omKeyPurgeRequest.preExecute(ozoneManager);
// Will not be equal, as UserInfo will be set.
Assert.assertNotEquals(originalOmRequest, modifiedOmRequest);
return modifiedOmRequest;
}
@Test
public void testValidateAndUpdateCacheCheckQuota() throws Exception {
// Create and Delete keys. The keys should be moved to DeletedKeys table
List<OmKeyInfo> deletedKeyInfos = createAndDeleteKeys(1, null);
// The keys should be present in the DeletedKeys table before purging
List<String> deletedKeyNames = new ArrayList<>();
for (OmKeyInfo deletedKey : deletedKeyInfos) {
String keyName = omMetadataManager.getOzoneKey(deletedKey.getVolumeName(),
deletedKey.getBucketName(), deletedKey.getKeyName());
Assert.assertTrue(omMetadataManager.getDeletedTable().isExist(
keyName));
deletedKeyNames.add(keyName);
}
// Create PurgeKeysRequest to purge the deleted keys
OMRequest omRequest = createPurgeKeysRequest(null, deletedKeyInfos);
OMRequest preExecutedRequest = preExecute(omRequest);
OMDirectoriesPurgeRequestWithFSO omKeyPurgeRequest =
new OMDirectoriesPurgeRequestWithFSO(preExecutedRequest);
String bucketKey = omMetadataManager.getBucketKey(volumeName, bucketName);
OmBucketInfo omBucketInfo = omMetadataManager.getBucketTable().get(
bucketKey);
Assert.assertEquals(1000L * deletedKeyNames.size(),
omBucketInfo.getUsedBytes());
OMDirectoriesPurgeResponseWithFSO omClientResponse
= (OMDirectoriesPurgeResponseWithFSO) omKeyPurgeRequest
.validateAndUpdateCache(ozoneManager, 100L,
ozoneManagerDoubleBufferHelper);
omBucketInfo = omMetadataManager.getBucketTable().get(
bucketKey);
Assert.assertEquals(0L * deletedKeyNames.size(),
omBucketInfo.getUsedBytes());
try (BatchOperation batchOperation =
omMetadataManager.getStore().initBatchOperation()) {
omClientResponse.addToDBBatch(omMetadataManager, batchOperation);
// Do manual commit and see whether addToBatch is successful or not.
omMetadataManager.getStore().commitBatchOperation(batchOperation);
}
// The keys should exist in the DeletedKeys table after dir delete
for (String deletedKey : deletedKeyNames) {
Assert.assertTrue(omMetadataManager.getDeletedTable().isExist(
deletedKey));
}
}
}