4
4
import com .marklogic .mgmt .AbstractResourceManager ;
5
5
import com .marklogic .mgmt .ManageClient ;
6
6
import com .marklogic .rest .util .Fragment ;
7
+ import org .springframework .web .client .HttpClientErrorException ;
7
8
8
9
import java .util .HashMap ;
9
10
import java .util .Iterator ;
@@ -28,6 +29,8 @@ public class ForestManager extends AbstractResourceManager {
28
29
public final static String REPLICAS_DELETE = "delete" ;
29
30
30
31
private String deleteLevel = DELETE_LEVEL_FULL ;
32
+ private int deleteRetryAttempts = 3 ;
33
+ private long deleteSleepPeriod = 500 ;
31
34
32
35
public ForestManager (ManageClient client ) {
33
36
super (client );
@@ -58,11 +61,32 @@ public void delete(String nameOrId, String level, String replicas) {
58
61
logger .info (format ("Could not find forest with name or ID: %s, so not deleting" , nameOrId ));
59
62
} else {
60
63
logger .info (format ("Deleting forest %s" , nameOrId ));
61
- getManageClient ().delete (format ("/manage/v2/forests/%s?level=%s&replicas=%s" , nameOrId , level , replicas ));
64
+ String path = format ("/manage/v2/forests/%s?level=%s&replicas=%s" , nameOrId , level , replicas );
65
+ deleteWithRetry (path , deleteRetryAttempts );
62
66
logger .info (format ("Deleted forest %s" , nameOrId ));
63
67
}
64
68
}
65
69
70
+ public void deleteWithRetry (String path , int attemptsLeft ) {
71
+ try {
72
+ getManageClient ().delete (path );
73
+ } catch (Exception e ) {
74
+ // Error will be logged automatically by MgmtResponseErrorHandler
75
+ if (attemptsLeft > 0 ) {
76
+ try {
77
+ logger .warn ("Unable to delete forest; will wait " + deleteSleepPeriod + "ms, then retry at path: " + path );
78
+ Thread .sleep (deleteSleepPeriod );
79
+ attemptsLeft --;
80
+ deleteWithRetry (path , attemptsLeft );
81
+ } catch (InterruptedException ex ) {
82
+ // Ignore
83
+ }
84
+ } else {
85
+ throw e ;
86
+ }
87
+ }
88
+ }
89
+
66
90
/**
67
91
* Supports either an array of JSON objects or a single JSON object.
68
92
*
@@ -199,4 +223,12 @@ public String getDeleteLevel() {
199
223
public void setDeleteLevel (String deleteLevel ) {
200
224
this .deleteLevel = deleteLevel ;
201
225
}
226
+
227
+ public void setDeleteRetryAttempts (int deleteRetryAttempts ) {
228
+ this .deleteRetryAttempts = deleteRetryAttempts ;
229
+ }
230
+
231
+ public void setDeleteSleepPeriod (long deleteSleepPeriod ) {
232
+ this .deleteSleepPeriod = deleteSleepPeriod ;
233
+ }
202
234
}
0 commit comments