3636import java .util .Set ;
3737import java .util .concurrent .ConcurrentHashMap ;
3838import java .util .concurrent .TimeUnit ;
39+ import java .util .concurrent .TimeoutException ;
3940import java .util .concurrent .atomic .AtomicInteger ;
4041import java .util .function .Consumer ;
4142import java .util .stream .Collectors ;
114115import org .apache .hugegraph .space .register .dto .ServiceDTO ;
115116import org .apache .hugegraph .space .register .registerImpl .PdRegister ;
116117import org .apache .hugegraph .task .TaskManager ;
118+ import org .apache .hugegraph .task .TaskScheduler ;
117119import org .apache .hugegraph .testutil .Whitebox ;
118120import org .apache .hugegraph .traversal .optimize .HugeScriptTraversal ;
119121import org .apache .hugegraph .type .define .CollectionType ;
@@ -1107,23 +1109,21 @@ public void getSpaceStorage(String graphSpace) {
11071109 }
11081110
11091111 public void clearGraphSpace (String name ) {
1110- // Clear all roles
1111- this .metaManager .clearGraphAuth (name );
1112-
1113- // Clear all schemaTemplate
1114- this .metaManager .clearSchemaTemplate (name );
1115-
11161112 // Clear all graphs
11171113 for (String key : this .graphs .keySet ()) {
1118- if (key .startsWith (name )) {
1114+ if (key .startsWith (name + DELIMITER )) {
11191115 String [] parts = key .split (DELIMITER );
11201116 this .dropGraph (parts [0 ], parts [1 ], true );
11211117 }
11221118 }
11231119
1120+ // Shared metadata remains available if a graph refuses deletion.
1121+ this .metaManager .clearGraphAuth (name );
1122+ this .metaManager .clearSchemaTemplate (name );
1123+
11241124 // Clear all services
11251125 for (String key : this .services .keySet ()) {
1126- if (key .startsWith (name )) {
1126+ if (key .startsWith (name + DELIMITER )) {
11271127 String [] parts = key .split (DELIMITER );
11281128 this .dropService (parts [0 ], parts [1 ]);
11291129 }
@@ -1457,12 +1457,13 @@ private HugeGraph createGraphLocal(HugeConfig config, String name) {
14571457 LOG .error ("Failed to create graph '{}' due to: {}" ,
14581458 name , e .getMessage (), e );
14591459 if (graph != null ) {
1460- this .graphs .remove (graph .spaceGraphName (), graph );
14611460 try {
14621461 this .dropGraphLocal (graph );
1463- } finally {
1464- // The create event may have partially registered the graph
1462+ this .graphs .remove (graph .spaceGraphName (), graph );
14651463 this .notifyEventLenient (Events .GRAPH_DROP , graph );
1464+ } catch (Throwable cleanup ) {
1465+ e .addSuppressed (cleanup );
1466+ this .graphs .putIfAbsent (graph .spaceGraphName (), graph );
14661467 }
14671468 }
14681469 throw e ;
@@ -1472,16 +1473,8 @@ private HugeGraph createGraphLocal(HugeConfig config, String name) {
14721473 }
14731474
14741475 private void dropGraphLocal (HugeGraph graph ) {
1475- try {
1476- // Clear data and config files
1477- graph .drop ();
1478- } finally {
1479- /*
1480- * Will fill graph instance into HugeFactory.graphs after
1481- * GraphFactory.open() succeed, remove it when the graph drops
1482- */
1483- HugeFactory .remove (graph );
1484- }
1476+ graph .drop ();
1477+ HugeFactory .remove (graph );
14851478 }
14861479
14871480 public HugeGraph createGraph (String graphSpace , String name , String creator ,
@@ -1614,16 +1607,14 @@ public HugeGraph createGraph(String graphSpace, String name, String creator,
16141607 try {
16151608 this .notifyEvent (Events .GRAPH_CREATE , graph );
16161609 } catch (Throwable e ) {
1617- this .notifyEventLenient (Events .GRAPH_DROP , graph );
1618- this .graphs .remove (graphName , graph );
16191610 try {
16201611 graph .close ();
1612+ this .notifyEventLenient (Events .GRAPH_DROP , graph );
1613+ this .graphs .remove (graphName , graph );
1614+ HugeFactory .remove (graph );
16211615 } catch (Exception e1 ) {
1622- if (graph instanceof StandardHugeGraph ) {
1623- ((StandardHugeGraph ) graph ).clearSchedulerAndLock ();
1624- }
1616+ e .addSuppressed (e1 );
16251617 }
1626- HugeFactory .remove (graph );
16271618 throw e ;
16281619 }
16291620
@@ -2086,12 +2077,11 @@ private HugeGraph createGraph(String graphSpace, HugeConfig config,
20862077 } catch (BackendException e ) {
20872078 try {
20882079 graph .close ();
2080+ HugeFactory .remove (graph );
20892081 } catch (Exception e1 ) {
2090- if (graph instanceof StandardHugeGraph ) {
2091- ((StandardHugeGraph ) graph ).clearSchedulerAndLock ();
2092- }
2082+ e .addSuppressed (e1 );
2083+ this .graphs .putIfAbsent (graph .spaceGraphName (), graph );
20932084 }
2094- HugeFactory .remove (graph );
20952085 throw e ;
20962086 }
20972087 }
@@ -2269,6 +2259,18 @@ public void dropGraph(String graphSpace, String name, boolean clear) {
22692259
22702260 String graphName = spaceGraphName (graphSpace , name );
22712261 if (clear ) {
2262+ // The auth proxy checks ADMIN before the local deletion fence.
2263+ TaskScheduler scheduler = g .taskScheduler ();
2264+ if (!scheduler .close ()) {
2265+ try {
2266+ scheduler .waitUntilAllTasksCompleted (g .configuration ().get (CoreOptions .TASK_WAIT_TIMEOUT ));
2267+ } catch (TimeoutException e ) {
2268+ throw new HugeException ("Tasks are still active in graph '%s', please retry later" ,
2269+ e , graphName );
2270+ }
2271+ E .checkState (scheduler .close (), "Tasks are still active in graph '%s', please retry later" ,
2272+ graphName );
2273+ }
22722274 this .removingGraphs .add (graphName );
22732275 try {
22742276 this .metaManager .removeGraphConfig (graphSpace , name );
@@ -2278,24 +2280,13 @@ public void dropGraph(String graphSpace, String name, boolean clear) {
22782280 "Failed to remove graph config of '%s'" , name , e );
22792281 }
22802282
2281- /**
2282- * close task scheduler before clear data,
2283- * because taskinfo stored in backend in
2284- * {@link org.apache.hugegraph.task.DistributedTaskScheduler}
2285- */
2286- try {
2287- g .taskScheduler ().close ();
2288- } catch (Throwable t ) {
2289- LOG .warn (String .format (
2290- "Error when close TaskScheduler of %s" ,
2291- graphName ),
2292- t );
2293- }
2294-
22952283 g .clearBackend ();
22962284 try {
22972285 g .close ();
22982286 } catch (Exception e ) {
2287+ if (!g .closed ()) {
2288+ throw new HugeException ("Failed to close graph '%s', please retry later" , e , graphName );
2289+ }
22992290 LOG .warn ("Failed to close graph" , e );
23002291 }
23012292 }
@@ -2309,13 +2300,17 @@ public void dropGraph(String graphSpace, String name, boolean clear) {
23092300 }
23102301 // Let gremlin server and rest server context remove graph
23112302 LOG .info ("Notify remove graph {} by GRAPH_DROP event" , name );
2312- Graph graph = this .graphs .remove (graphName );
2303+ Graph graph = this .graphs .get (graphName );
23132304 if (graph != null ) {
23142305 try {
23152306 graph .close ();
23162307 } catch (Exception e ) {
2308+ if (!((HugeGraph ) graph ).closed ()) {
2309+ throw new HugeException ("Failed to close graph '%s', please retry later" , e , graphName );
2310+ }
23172311 LOG .warn ("Failed to close graph" , e );
23182312 }
2313+ this .graphs .remove (graphName , graph );
23192314 try {
23202315 // Delete alias graph in HugeFactory
23212316 HugeFactory .remove ((HugeGraph ) graph );
0 commit comments