-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathIndexCleanupUtility.java
More file actions
353 lines (303 loc) · 13.3 KB
/
IndexCleanupUtility.java
File metadata and controls
353 lines (303 loc) · 13.3 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package io.pinecone.helpers;
import io.pinecone.clients.Pinecone;
import org.openapitools.db_control.client.model.CollectionList;
import org.openapitools.db_control.client.model.CollectionModel;
import org.openapitools.db_control.client.model.IndexList;
import org.openapitools.db_control.client.model.IndexModel;
import java.util.ArrayList;
import java.util.List;
/**
* Utility for cleaning up Pinecone indexes and collections.
*
* This utility can be used to clean up resources in a Pinecone project, with support for:
* - Deleting all indexes and collections
* - Dry-run mode to preview deletions without executing them
* - Automatic handling of deletion protection
* - Age-based filtering (when timestamp information becomes available)
*
* Command-line arguments:
* --age-threshold-days <number> - Minimum age in days for resources to be deleted (default: 1)
* --dry-run - Preview deletions without actually deleting resources
*
* Example usage:
* <pre>{@code
* java io.pinecone.helpers.IndexCleanupUtility --age-threshold-days 2 --dry-run
* }</pre>
*/
public class IndexCleanupUtility {
private final Pinecone pinecone;
private final int ageThresholdDays;
private final boolean dryRun;
/**
* Constructs a new IndexCleanupUtility.
*
* @param pinecone The Pinecone client instance
* @param ageThresholdDays Minimum age in days for resources to be deleted
* @param dryRun If true, preview deletions without executing them
*/
public IndexCleanupUtility(Pinecone pinecone, int ageThresholdDays, boolean dryRun) {
this.pinecone = pinecone;
this.ageThresholdDays = ageThresholdDays;
this.dryRun = dryRun;
}
/**
* Main entry point for the cleanup utility.
*
* @param args Command-line arguments
*/
public static void main(String[] args) {
try {
ParsedArgs parsedArgs = parseArgs(args);
logInfo("Starting Pinecone resource cleanup...");
logInfo("Age threshold: %d days", parsedArgs.ageThresholdDays);
logInfo("Dry-run mode: %s", parsedArgs.dryRun);
// Initialize Pinecone client
String apiKey = System.getenv("PINECONE_API_KEY");
if (apiKey == null || apiKey.isEmpty()) {
logError("PINECONE_API_KEY environment variable is not set");
System.exit(1);
}
Pinecone pinecone = new Pinecone.Builder(apiKey).build();
IndexCleanupUtility utility = new IndexCleanupUtility(
pinecone,
parsedArgs.ageThresholdDays,
parsedArgs.dryRun
);
// Execute cleanup
CleanupResult result = utility.cleanup();
// Log summary
logInfo("=== Cleanup Summary ===");
logInfo("Indexes processed: %d", result.getIndexesProcessed());
logInfo("Indexes deleted: %d", result.getIndexesDeleted());
logInfo("Indexes failed: %d", result.getIndexesFailed());
logInfo("Collections processed: %d", result.getCollectionsProcessed());
logInfo("Collections deleted: %d", result.getCollectionsDeleted());
logInfo("Collections failed: %d", result.getCollectionsFailed());
if (parsedArgs.dryRun) {
logInfo("DRY-RUN MODE: No resources were actually deleted");
}
logInfo("Cleanup completed");
// Exit with error code if any deletions failed
if (result.getIndexesFailed() > 0 || result.getCollectionsFailed() > 0) {
System.exit(1);
}
} catch (Exception e) {
logError("Error during cleanup: " + e.getMessage(), e);
System.exit(1);
}
}
static ParsedArgs parseArgs(String[] args) {
int ageThresholdDays = 1; // Default: 1 day
boolean dryRun = false;
for (int i = 0; i < args.length; i++) {
if ("--age-threshold-days".equals(args[i])) {
if (i + 1 >= args.length) {
throw new IllegalArgumentException("--age-threshold-days requires a value");
}
String value = args[i + 1];
try {
ageThresholdDays = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid value for --age-threshold-days: " + value, e);
}
if (ageThresholdDays < 0) {
throw new IllegalArgumentException("--age-threshold-days must be >= 0");
}
i++; // Skip the next argument since we've consumed it
} else if ("--dry-run".equals(args[i])) {
dryRun = true;
} else {
logWarn("Unknown argument: %s", args[i]);
}
}
return new ParsedArgs(ageThresholdDays, dryRun);
}
private static void printUsage() {
System.err.println("Usage: java io.pinecone.helpers.IndexCleanupUtility [OPTIONS]");
System.err.println("Options:");
System.err.println(" --age-threshold-days <number> Minimum age in days for resources to be deleted (default: 1)");
System.err.println(" --dry-run Preview deletions without executing them");
}
/**
* Executes the cleanup operation, deleting indexes and collections that match the criteria.
*
* @return A CleanupResult containing statistics about the cleanup operation
* @throws Exception if an error occurs during cleanup
*/
public CleanupResult cleanup() throws Exception {
CleanupResult result = new CleanupResult();
// Clean up indexes
logInfo("Listing indexes...");
IndexList indexList = pinecone.listIndexes();
List<IndexModel> indexes = indexList != null ? indexList.getIndexes() : null;
if (indexes == null) {
indexes = new ArrayList<>();
}
logInfo("Found %d indexes", indexes.size());
for (IndexModel index : indexes) {
result.incrementIndexesProcessed();
try {
boolean deletionInitiated = cleanupIndex(index);
if (deletionInitiated) {
result.incrementIndexesDeleted();
}
} catch (Exception e) {
logError(String.format("Failed to delete index %s: %s", index.getName(), e.getMessage()), e);
result.incrementIndexesFailed();
}
}
// Clean up collections
logInfo("Listing collections...");
CollectionList collectionList = pinecone.listCollections();
List<CollectionModel> collections = collectionList != null ? collectionList.getCollections() : null;
if (collections == null) {
collections = new ArrayList<>();
}
logInfo("Found %d collections", collections.size());
for (CollectionModel collection : collections) {
result.incrementCollectionsProcessed();
try {
boolean deletionInitiated = cleanupCollection(collection);
if (deletionInitiated) {
result.incrementCollectionsDeleted();
}
} catch (Exception e) {
logError(String.format("Failed to delete collection %s: %s", collection.getName(), e.getMessage()), e);
result.incrementCollectionsFailed();
}
}
return result;
}
/**
* Cleans up a single index.
*
* @param index The index to clean up
* @throws Exception if an error occurs during cleanup
*/
private boolean cleanupIndex(IndexModel index) throws Exception {
String indexName = index.getName();
String status = index.getStatus() != null && index.getStatus().getState() != null
? index.getStatus().getState()
: "unknown";
logInfo("Processing index: %s (status: %s)", indexName, status);
// Skip indexes that are already terminating
if ("Terminating".equalsIgnoreCase(status)) {
logInfo("Skipping index %s - already terminating", indexName);
return false;
}
// Note: Age-based filtering would go here when timestamp information becomes available
// For now, we process all indexes that aren't already terminating
if (dryRun) {
logInfo("DRY-RUN: Would delete index: %s", indexName);
return false;
}
// Handle deletion protection
if ("enabled".equals(index.getDeletionProtection())) {
logInfo("Index %s has deletion protection enabled, disabling...", indexName);
try {
// Try pod-based configuration first
index.getSpec().getIndexModelPodBased();
pinecone.configurePodsIndex(indexName, "disabled");
} catch (ClassCastException e) {
// Not a pod-based index, try serverless
pinecone.configureServerlessIndex(indexName, "disabled", null, null);
}
// Wait for configuration to take effect
logInfo("Waiting 5 seconds for deletion protection to be disabled...");
Thread.sleep(5000);
}
// Delete the index
logInfo("Deleting index: %s", indexName);
pinecone.deleteIndex(indexName);
logInfo("Successfully initiated deletion of index: %s", indexName);
// Add delay to avoid overwhelming the backend
logInfo("Waiting 30 seconds before next deletion...");
Thread.sleep(30000);
return true;
}
/**
* Cleans up a single collection.
*
* @param collection The collection to clean up
* @throws Exception if an error occurs during cleanup
*/
private boolean cleanupCollection(CollectionModel collection) throws Exception {
String collectionName = collection.getName();
String status = collection.getStatus() != null ? collection.getStatus() : "unknown";
logInfo("Processing collection: %s (status: %s)", collectionName, status);
// Skip collections that are already terminating
if ("Terminating".equalsIgnoreCase(status)) {
logInfo("Skipping collection %s - already terminating", collectionName);
return false;
}
// Note: Age-based filtering would go here when timestamp information becomes available
// For now, we process all collections that aren't already terminating
if (dryRun) {
logInfo("DRY-RUN: Would delete collection: %s", collectionName);
return false;
}
// Delete the collection
logInfo("Deleting collection: %s", collectionName);
pinecone.deleteCollection(collectionName);
logInfo("Successfully initiated deletion of collection: %s", collectionName);
// Add delay to avoid overwhelming the backend
logInfo("Waiting 30 seconds before next deletion...");
Thread.sleep(30000);
return true;
}
/**
* Result of a cleanup operation, containing statistics about what was processed and deleted.
*/
public static class CleanupResult {
private int indexesProcessed = 0;
private int indexesDeleted = 0;
private int indexesFailed = 0;
private int collectionsProcessed = 0;
private int collectionsDeleted = 0;
private int collectionsFailed = 0;
public int getIndexesProcessed() { return indexesProcessed; }
public int getIndexesDeleted() { return indexesDeleted; }
public int getIndexesFailed() { return indexesFailed; }
public int getCollectionsProcessed() { return collectionsProcessed; }
public int getCollectionsDeleted() { return collectionsDeleted; }
public int getCollectionsFailed() { return collectionsFailed; }
void incrementIndexesProcessed() { indexesProcessed++; }
void incrementIndexesDeleted() { indexesDeleted++; }
void incrementIndexesFailed() { indexesFailed++; }
void incrementCollectionsProcessed() { collectionsProcessed++; }
void incrementCollectionsDeleted() { collectionsDeleted++; }
void incrementCollectionsFailed() { collectionsFailed++; }
}
static final class ParsedArgs {
final int ageThresholdDays;
final boolean dryRun;
ParsedArgs(int ageThresholdDays, boolean dryRun) {
this.ageThresholdDays = ageThresholdDays;
this.dryRun = dryRun;
}
}
private static void logInfo(String format, Object... args) {
log("INFO", format, args);
}
private static void logWarn(String format, Object... args) {
log("WARN", format, args);
}
private static void logError(String message) {
System.err.println("[ERROR] " + message);
}
private static void logError(String message, Throwable t) {
System.err.println("[ERROR] " + message);
if (t != null) {
t.printStackTrace(System.err);
}
}
private static void log(String level, String format, Object... args) {
String msg;
try {
msg = (args == null || args.length == 0) ? format : String.format(format, args);
} catch (Exception e) {
msg = format;
}
System.err.println("[" + level + "] " + msg);
}
}