HADOOP-18691. Add a CallerContext getter on the Schedulable interface (#5540)

This commit is contained in:
Christos Bisias 2023-04-20 20:11:25 +03:00 committed by GitHub
parent 0918c87fa2
commit 9e24ed2196
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 5 deletions

View File

@ -29,5 +29,19 @@
public interface Schedulable { public interface Schedulable {
public UserGroupInformation getUserGroupInformation(); public UserGroupInformation getUserGroupInformation();
/**
* This is overridden only in {@link Server.Call}.
* The CallerContext field will be used to carry information
* about the user in cases where UGI proves insufficient.
* Any other classes that might try to use this method,
* will get an UnsupportedOperationException.
*
* @return an instance of CallerContext if method
* is overridden else get an UnsupportedOperationException
*/
default CallerContext getCallerContext() {
throw new UnsupportedOperationException("Invalid operation.");
}
int getPriorityLevel(); int getPriorityLevel();
} }

View File

@ -1086,6 +1086,11 @@ public UserGroupInformation getUserGroupInformation() {
return getRemoteUser(); return getRemoteUser();
} }
@Override
public CallerContext getCallerContext() {
return this.callerContext;
}
@Override @Override
public int getPriorityLevel() { public int getPriorityLevel() {
return this.priorityLevel; return this.priorityLevel;

View File

@ -20,8 +20,9 @@
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.assertj.core.api.Assertions.assertThat;
import org.apache.hadoop.test.LambdaTestUtils;
import org.junit.Test; import org.junit.Test;
import java.util.List; import java.util.List;
@ -33,7 +34,7 @@
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
public class TestIdentityProviders { public class TestIdentityProviders {
public class FakeSchedulable implements Schedulable { public static class FakeSchedulable implements Schedulable {
public FakeSchedulable() { public FakeSchedulable() {
} }
@ -61,7 +62,9 @@ public void testPluggableIdentityProvider() {
CommonConfigurationKeys.IPC_IDENTITY_PROVIDER_KEY, CommonConfigurationKeys.IPC_IDENTITY_PROVIDER_KEY,
IdentityProvider.class); IdentityProvider.class);
assertTrue(providers.size() == 1); assertThat(providers)
.describedAs("provider list")
.hasSize(1);
IdentityProvider ip = providers.get(0); IdentityProvider ip = providers.get(0);
assertNotNull(ip); assertNotNull(ip);
@ -69,14 +72,20 @@ public void testPluggableIdentityProvider() {
} }
@Test @Test
public void testUserIdentityProvider() throws IOException { public void testUserIdentityProvider() throws Exception {
UserIdentityProvider uip = new UserIdentityProvider(); UserIdentityProvider uip = new UserIdentityProvider();
String identity = uip.makeIdentity(new FakeSchedulable()); FakeSchedulable fakeSchedulable = new FakeSchedulable();
String identity = uip.makeIdentity(fakeSchedulable);
// Get our username // Get our username
UserGroupInformation ugi = UserGroupInformation.getCurrentUser(); UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
String username = ugi.getUserName(); String username = ugi.getUserName();
assertEquals(username, identity); assertEquals(username, identity);
// FakeSchedulable doesn't override getCallerContext()
// accessing it should throw an UnsupportedOperationException
LambdaTestUtils.intercept(UnsupportedOperationException.class,
"Invalid operation.", fakeSchedulable::getCallerContext);
} }
} }