2020package org .elasticsearch .hadoop .rest .pooling ;
2121
2222import org .elasticsearch .hadoop .cfg .Settings ;
23+ import org .elasticsearch .hadoop .mr .security .HadoopUserProvider ;
2324import org .elasticsearch .hadoop .rest .Transport ;
2425import org .elasticsearch .hadoop .security .SecureSettings ;
2526import org .elasticsearch .hadoop .util .SettingsUtils ;
2930import java .util .UUID ;
3031
3132import static org .elasticsearch .hadoop .cfg .ConfigurationOptions .ES_NET_TRANSPORT_POOLING_EXPIRATION_TIMEOUT ;
33+ import static org .elasticsearch .hadoop .cfg .ConfigurationOptions .ES_SECURITY_USER_PROVIDER_CLASS ;
3234
3335public class AbstractTransportPoolTest {
3436
@@ -77,4 +79,78 @@ public void removeOldConnections() throws Exception {
7779 }
7880 }
7981
82+
83+ @ Test
84+ public void testValidationWithNoUserProvider () throws Exception {
85+ /*
86+ * In this test, we make sure that validating PooledTransports does not throw an exception if the resource is bad. Instead, the
87+ * PooledTransport is replaced.
88+ */
89+ Settings settings = new TestSettings ();
90+ settings .setProperty (ES_NET_TRANSPORT_POOLING_EXPIRATION_TIMEOUT , "1s" );
91+ String host = SettingsUtils .discoveredOrDeclaredNodes (settings ).get (0 );
92+
93+ TransportPool pool = new TransportPool (UUID .randomUUID ().toString (), host , settings , new SecureSettings (settings ));
94+
95+ Transport transport1 = null ;
96+
97+ try {
98+ transport1 = pool .borrowTransport ();
99+ transport1 .close ();
100+
101+ // Wait the amount of time to close.
102+ Thread .sleep (settings .getTransportPoolingExpirationTimeout ().millis () + 1000L );
103+
104+ transport1 = pool .borrowTransport ();
105+ transport1 .close ();
106+
107+ // Will need to remove 2 connections at this point
108+ pool .removeOldConnections ();
109+
110+ } finally {
111+ // Close everything
112+ if (transport1 != null ) {
113+ transport1 .close ();
114+ }
115+ }
116+
117+ }
118+
119+ @ Test
120+ public void testValidationWithUserProvider () throws Exception {
121+ /*
122+ * In this test, we make sure that validating PooledTransports does not throw an exception if the resource is bad when we have set
123+ * a ES_SECURITY_USER_PROVIDER_CLASS. Instead, the PooledTransport is replaced. Previously this would incorrectly throw an
124+ * exception.
125+ */
126+ Settings settings = new TestSettings ();
127+ settings .setProperty (ES_NET_TRANSPORT_POOLING_EXPIRATION_TIMEOUT , "1s" );
128+ settings .setProperty (ES_SECURITY_USER_PROVIDER_CLASS , HadoopUserProvider .class .getName ());
129+ String badHost = "127.0.0.1:11111" ; //intentionally not a real host so that validation fails
130+
131+ TransportPool pool = new TransportPool (UUID .randomUUID ().toString (), badHost , settings , new SecureSettings (settings ));
132+
133+ Transport transport1 = null ;
134+
135+ try {
136+ transport1 = pool .borrowTransport ();
137+ transport1 .close ();
138+
139+ // Wait the amount of time to close.
140+ Thread .sleep (settings .getTransportPoolingExpirationTimeout ().millis () + 1000L );
141+
142+ // The following will throw an exception reported in https://github.com/elastic/elasticsearch-hadoop/issues/1362 without the fix
143+ transport1 = pool .borrowTransport ();
144+ transport1 .close ();
145+
146+ // Will need to remove 2 connections at this point
147+ pool .removeOldConnections ();
148+
149+ } finally {
150+ // Close everything
151+ if (transport1 != null ) {
152+ transport1 .close ();
153+ }
154+ }
155+ }
80156}
0 commit comments