66
77package org .elasticsearch .xpack .enrich .action ;
88
9+ import org .elasticsearch .ResourceNotFoundException ;
910import org .elasticsearch .action .ActionListener ;
1011import org .elasticsearch .action .support .master .AcknowledgedResponse ;
1112import org .elasticsearch .cluster .service .ClusterService ;
1213import org .elasticsearch .common .util .concurrent .EsRejectedExecutionException ;
1314import org .elasticsearch .common .xcontent .XContentType ;
15+ import org .elasticsearch .index .IndexNotFoundException ;
1416import org .elasticsearch .xpack .core .enrich .EnrichPolicy ;
1517import org .elasticsearch .xpack .core .enrich .action .DeleteEnrichPolicyAction ;
1618import org .elasticsearch .xpack .enrich .AbstractEnrichTestCase ;
1719import org .elasticsearch .xpack .enrich .EnrichPolicyLocks ;
20+ import org .elasticsearch .xpack .enrich .EnrichStore ;
21+ import org .junit .After ;
1822
1923import java .util .concurrent .CountDownLatch ;
2024import java .util .concurrent .atomic .AtomicReference ;
2428import static org .hamcrest .Matchers .nullValue ;
2529import static org .hamcrest .core .IsInstanceOf .instanceOf ;
2630
27- public class TransportDeleteEnricyPolicyActionTests extends AbstractEnrichTestCase {
31+ public class TransportDeleteEnrichPolicyActionTests extends AbstractEnrichTestCase {
2832
29- public void testDeleteIsNotLocked () throws InterruptedException {
33+ @ After
34+ private void cleanupPolicy () {
35+ ClusterService clusterService = getInstanceFromNode (ClusterService .class );
36+ String name = "my-policy" ;
37+
38+ try {
39+ deleteEnrichPolicy (name , clusterService );
40+ } catch (Exception e ) {
41+ // if the enrich policy does not exist, then just keep going
42+ }
43+
44+ // fail if the state of this is left locked
45+ EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
46+ assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
47+ }
48+
49+ public void testDeletePolicyDoesNotExistUnlocksPolicy () throws InterruptedException {
50+ String fakeId = "fake-id" ;
51+ createIndex (EnrichPolicy .getBaseName (fakeId ) + "-foo1" );
52+ createIndex (EnrichPolicy .getBaseName (fakeId ) + "-foo2" );
53+
54+ final CountDownLatch latch = new CountDownLatch (1 );
55+ final AtomicReference <Exception > reference = new AtomicReference <>();
56+ final TransportDeleteEnrichPolicyAction transportAction = node ().injector ().getInstance (TransportDeleteEnrichPolicyAction .class );
57+ transportAction .execute (null ,
58+ new DeleteEnrichPolicyAction .Request (fakeId ),
59+ new ActionListener <AcknowledgedResponse >() {
60+ @ Override
61+ public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
62+ fail ();
63+ }
64+
65+ public void onFailure (final Exception e ) {
66+ reference .set (e );
67+ latch .countDown ();
68+ }
69+ });
70+ latch .await ();
71+ assertNotNull (reference .get ());
72+ assertThat (reference .get (), instanceOf (ResourceNotFoundException .class ));
73+ assertThat (reference .get ().getMessage (), equalTo ("policy [fake-id] not found" ));
74+
75+ // fail if the state of this is left locked
76+ EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
77+ assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
78+ }
79+
80+ public void testDeleteWithoutIndex () throws Exception {
3081 EnrichPolicy policy = randomEnrichPolicy (XContentType .JSON );
3182 ClusterService clusterService = getInstanceFromNode (ClusterService .class );
3283 String name = "my-policy" ;
@@ -53,6 +104,56 @@ public void onFailure(final Exception e) {
53104 latch .await ();
54105 assertNotNull (reference .get ());
55106 assertTrue (reference .get ().isAcknowledged ());
107+
108+ EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
109+ assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
110+
111+ assertNull (EnrichStore .getPolicy (name , clusterService .state ()));
112+ }
113+
114+ public void testDeleteIsNotLocked () throws Exception {
115+ EnrichPolicy policy = randomEnrichPolicy (XContentType .JSON );
116+ ClusterService clusterService = getInstanceFromNode (ClusterService .class );
117+ String name = "my-policy" ;
118+
119+ AtomicReference <Exception > error = saveEnrichPolicy (name , policy , clusterService );
120+ assertThat (error .get (), nullValue ());
121+
122+ createIndex (EnrichPolicy .getBaseName (name ) + "-foo1" );
123+ createIndex (EnrichPolicy .getBaseName (name ) + "-foo2" );
124+
125+ client ().admin ().indices ().prepareGetIndex ().setIndices (
126+ EnrichPolicy .getBaseName (name ) + "-foo1" ,
127+ EnrichPolicy .getBaseName (name ) + "-foo2" ).get ();
128+
129+ final CountDownLatch latch = new CountDownLatch (1 );
130+ final AtomicReference <AcknowledgedResponse > reference = new AtomicReference <>();
131+ final TransportDeleteEnrichPolicyAction transportAction = node ().injector ().getInstance (TransportDeleteEnrichPolicyAction .class );
132+ transportAction .execute (null ,
133+ new DeleteEnrichPolicyAction .Request (name ),
134+ new ActionListener <AcknowledgedResponse >() {
135+ @ Override
136+ public void onResponse (AcknowledgedResponse acknowledgedResponse ) {
137+ reference .set (acknowledgedResponse );
138+ latch .countDown ();
139+ }
140+
141+ public void onFailure (final Exception e ) {
142+ fail ();
143+ }
144+ });
145+ latch .await ();
146+ assertNotNull (reference .get ());
147+ assertTrue (reference .get ().isAcknowledged ());
148+
149+ expectThrows (IndexNotFoundException .class , () -> client ().admin ().indices ().prepareGetIndex ().setIndices (
150+ EnrichPolicy .getBaseName (name ) + "-foo1" ,
151+ EnrichPolicy .getBaseName (name ) + "-foo2" ).get ());
152+
153+ EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
154+ assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
155+
156+ assertNull (EnrichStore .getPolicy (name , clusterService .state ()));
56157 }
57158
58159 public void testDeleteLocked () throws InterruptedException {
@@ -63,6 +164,9 @@ public void testDeleteLocked() throws InterruptedException {
63164 AtomicReference <Exception > error = saveEnrichPolicy (name , policy , clusterService );
64165 assertThat (error .get (), nullValue ());
65166
167+ createIndex (EnrichPolicy .getBaseName (name ) + "-foo1" );
168+ createIndex (EnrichPolicy .getBaseName (name ) + "-foo2" );
169+
66170 EnrichPolicyLocks enrichPolicyLocks = getInstanceFromNode (EnrichPolicyLocks .class );
67171 assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
68172
@@ -116,6 +220,8 @@ public void onFailure(final Exception e) {
116220 assertTrue (reference .get ().isAcknowledged ());
117221
118222 assertFalse (enrichPolicyLocks .captureExecutionState ().isAnyPolicyInFlight ());
223+
224+ assertNull (EnrichStore .getPolicy (name , clusterService .state ()));
119225 }
120226 }
121227}
0 commit comments