44import com .marklogic .mgmt .AbstractResourceManager ;
55import com .marklogic .mgmt .ManageClient ;
66import com .marklogic .rest .util .Fragment ;
7+ import org .springframework .web .client .HttpClientErrorException ;
78
89import java .util .HashMap ;
910import java .util .Iterator ;
@@ -28,6 +29,8 @@ public class ForestManager extends AbstractResourceManager {
2829 public final static String REPLICAS_DELETE = "delete" ;
2930
3031 private String deleteLevel = DELETE_LEVEL_FULL ;
32+ private int deleteRetryAttempts = 3 ;
33+ private long deleteSleepPeriod = 500 ;
3134
3235 public ForestManager (ManageClient client ) {
3336 super (client );
@@ -58,11 +61,32 @@ public void delete(String nameOrId, String level, String replicas) {
5861 logger .info (format ("Could not find forest with name or ID: %s, so not deleting" , nameOrId ));
5962 } else {
6063 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 );
6266 logger .info (format ("Deleted forest %s" , nameOrId ));
6367 }
6468 }
6569
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+
6690 /**
6791 * Supports either an array of JSON objects or a single JSON object.
6892 *
@@ -199,4 +223,12 @@ public String getDeleteLevel() {
199223 public void setDeleteLevel (String deleteLevel ) {
200224 this .deleteLevel = deleteLevel ;
201225 }
226+
227+ public void setDeleteRetryAttempts (int deleteRetryAttempts ) {
228+ this .deleteRetryAttempts = deleteRetryAttempts ;
229+ }
230+
231+ public void setDeleteSleepPeriod (long deleteSleepPeriod ) {
232+ this .deleteSleepPeriod = deleteSleepPeriod ;
233+ }
202234}
0 commit comments