/** * Returns a 'priority' based on the request type. * * Currently the returned priority is used for queue selection. * See the SimpleRpcScheduler as example. It maintains a queue per 'priory type' * HIGH_QOS (meta requests), REPLICATION_QOS (replication requests), * NORMAL_QOS (user requests). */ @Override public int getPriority(RequestHeader header, Message param, User user) { int priorityByAnnotation = getAnnotatedPriority(header); if (priorityByAnnotation >= 0) { return priorityByAnnotation; } // all requests executed by super users have high QoS try { if (Superusers.isSuperUser(user)) { return HConstants.ADMIN_QOS; } } catch (IllegalStateException ex) { // Not good throwing an exception out of here, a runtime anyways. Let the query go into the // server and have it throw the exception if still an issue. Just mark it normal priority. if (LOG.isTraceEnabled()) LOG.trace("Marking normal priority after getting exception=" + ex); return HConstants.NORMAL_QOS; } return getBasePriority(header, param); }
/** * Based on the request content, returns the deadline of the request. * * @param header * @param param * @return Deadline of this request. 0 now, otherwise msec of 'delay' */ @Override public long getDeadline(RequestHeader header, Message param) { if (param instanceof ScanRequest) { ScanRequest request = (ScanRequest)param; if (!request.hasScannerId()) { return 0; } // get the 'virtual time' of the scanner, and applies sqrt() to get a // nice curve for the delay. More a scanner is used the less priority it gets. // The weight is used to have more control on the delay. long vtime = rpcServices.getScannerVirtualTime(request.getScannerId()); return Math.round(Math.sqrt(vtime * scanVirtualTimeWeight)); } return 0; }
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH", justification="Can't figure why this complaint is happening... see below") Call(int id, final BlockingService service, final MethodDescriptor md, RequestHeader header, Message param, CellScanner cellScanner, Connection connection, Responder responder, long size, TraceInfo tinfo, final InetAddress remoteAddress) { this.id = id; this.service = service; this.md = md; this.header = header; this.param = param; this.cellScanner = cellScanner; this.connection = connection; this.timestamp = System.currentTimeMillis(); this.response = null; this.responder = responder; this.isError = false; this.size = size; this.tinfo = tinfo; this.user = connection == null? null: connection.user; // FindBugs: NP_NULL_ON_SOME_PATH this.remoteAddress = remoteAddress; this.retryImmediatelySupported = connection == null? null: connection.retryImmediatelySupported; }
@Test public void testQosFunctionForRequestCalledBySuperUser() throws Exception { RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); headerBuilder.setMethodName("foo"); RequestHeader header = headerBuilder.build(); PriorityFunction qosFunc = regionServer.rpcServices.getPriority(); //test superusers regionServer.conf.set(Superusers.SUPERUSER_CONF_KEY, "samplesuperuser"); Superusers.initialize(regionServer.conf); assertEquals(HConstants.ADMIN_QOS, qosFunc.getPriority(header, null, User.createUserForTesting(regionServer.conf, "samplesuperuser", new String[]{"somegroup"}))); //test supergroups regionServer.conf.set(Superusers.SUPERUSER_CONF_KEY, "@samplesupergroup"); Superusers.initialize(regionServer.conf); assertEquals(HConstants.ADMIN_QOS, qosFunc.getPriority(header, null, User.createUserForTesting(regionServer.conf, "regularuser", new String[]{"samplesupergroup"}))); }
/** * Based on the request content, returns the deadline of the request. * * @param header * @param param * @return Deadline of this request. 0 now, otherwise msec of 'delay' */ @Override public long getDeadline(RequestHeader header, Message param) { String methodName = header.getMethodName(); if (methodName.equalsIgnoreCase("scan")) { ScanRequest request = (ScanRequest)param; if (!request.hasScannerId()) { return 0; } // get the 'virtual time' of the scanner, and applies sqrt() to get a // nice curve for the delay. More a scanner is used the less priority it gets. // The weight is used to have more control on the delay. long vtime = rpcServices.getScannerVirtualTime(request.getScannerId()); return Math.round(Math.sqrt(vtime * scanVirtualTimeWeight)); } return 0; }
Call(int id, final BlockingService service, final MethodDescriptor md, RequestHeader header, Message param, CellScanner cellScanner, Connection connection, Responder responder, long size, TraceInfo tinfo) { this.id = id; this.service = service; this.md = md; this.header = header; this.param = param; this.cellScanner = cellScanner; this.connection = connection; this.timestamp = System.currentTimeMillis(); this.response = null; this.delayResponse = false; this.responder = responder; this.isError = false; this.size = size; this.tinfo = tinfo; }
private boolean isWriteRequest(final RequestHeader header, final Message param) { // TODO: Is there a better way to do this? String methodName = header.getMethodName(); if (methodName.equalsIgnoreCase("multi") && param instanceof MultiRequest) { MultiRequest multi = (MultiRequest)param; for (RegionAction regionAction : multi.getRegionActionList()) { for (Action action: regionAction.getActionList()) { if (action.hasMutation()) { return true; } } } } if (methodName.equalsIgnoreCase("mutate")) { return true; } return false; }
/** * See if the method has an annotation. * @param header * @return Return the priority from the annotation. If there isn't * an annotation, this returns something below zero. */ protected int getAnnotatedPriority(RequestHeader header) { String methodName = header.getMethodName(); Integer priorityByAnnotation = annotatedQos.get(methodName); if (priorityByAnnotation != null) { return priorityByAnnotation; } return -1; }
private boolean isWriteRequest(final RequestHeader header, final Message param) { // TODO: Is there a better way to do this? if (param instanceof MultiRequest) { MultiRequest multi = (MultiRequest)param; for (RegionAction regionAction : multi.getRegionActionList()) { for (Action action: regionAction.getActionList()) { if (action.hasMutation()) { return true; } } } } if (param instanceof MutateRequest) { return true; } // Below here are methods for master. It's a pretty brittle version of this. // Not sure that master actually needs a read/write queue since 90% of requests to // master are writing to status or changing the meta table. // All other read requests are admin generated and can be processed whenever. // However changing that would require a pretty drastic change and should be done for // the next major release and not as a fix for HBASE-14239 if (param instanceof RegionServerStatusProtos.ReportRegionStateTransitionRequest) { return true; } if (param instanceof RegionServerStatusProtos.RegionServerStartupRequest) { return true; } if (param instanceof RegionServerStatusProtos.RegionServerReportRequest) { return true; } return false; }
private boolean isScanRequest(final RequestHeader header, final Message param) { if (param instanceof ScanRequest) { // The first scan request will be executed as a "short read" ScanRequest request = (ScanRequest)param; return request.hasScannerId(); } return false; }
@Test public void testQosFunctionForMeta() throws IOException { priority = regionServer.rpcServices.getPriority(); RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); //create a rpc request that has references to hbase:meta region and also //uses one of the known argument classes (known argument classes are //listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) headerBuilder.setMethodName("foo"); GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); ByteString name = ByteStringer.wrap(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); regionSpecifierBuilder.setValue(name); RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); getRequestBuilder.setRegion(regionSpecifier); Get.Builder getBuilder = Get.newBuilder(); getBuilder.setRow(ByteStringer.wrap("somerow".getBytes())); getRequestBuilder.setGet(getBuilder.build()); GetRequest getRequest = getRequestBuilder.build(); RequestHeader header = headerBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRpc.getRegion((RegionSpecifier) Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, getRequest, User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"}))); }
@Test public void testQosFunctionWithoutKnownArgument() throws IOException { //The request is not using any of the //known argument classes (it uses one random request class) //(known argument classes are listed in //HRegionServer.QosFunctionImpl.knownArgumentClasses) RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); headerBuilder.setMethodName("foo"); RequestHeader header = headerBuilder.build(); PriorityFunction qosFunc = regionServer.rpcServices.getPriority(); assertEquals(HConstants.NORMAL_QOS, qosFunc.getPriority(header, null, User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"}))); }
private boolean isScanRequest(final RequestHeader header, final Message param) { String methodName = header.getMethodName(); if (methodName.equalsIgnoreCase("scan")) { // The first scan request will be executed as a "short read" ScanRequest request = (ScanRequest)param; return request.hasScannerId(); } return false; }
@Test public void testQosFunctionForMeta() throws IOException { priority = regionServer.rpcServices.getPriority(); RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); //create a rpc request that has references to hbase:meta region and also //uses one of the known argument classes (known argument classes are //listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) headerBuilder.setMethodName("foo"); GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); ByteString name = ByteStringer.wrap(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); regionSpecifierBuilder.setValue(name); RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); getRequestBuilder.setRegion(regionSpecifier); Get.Builder getBuilder = Get.newBuilder(); getBuilder.setRow(ByteStringer.wrap("somerow".getBytes())); getRequestBuilder.setGet(getBuilder.build()); GetRequest getRequest = getRequestBuilder.build(); RequestHeader header = headerBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, getRequest)); }
@Test public void testQosFunctionWithoutKnownArgument() throws IOException { //The request is not using any of the //known argument classes (it uses one random request class) //(known argument classes are listed in //HRegionServer.QosFunctionImpl.knownArgumentClasses) RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); headerBuilder.setMethodName("foo"); RequestHeader header = headerBuilder.build(); PriorityFunction qosFunc = regionServer.rpcServices.getPriority(); assertEquals(HConstants.NORMAL_QOS, qosFunc.getPriority(header, null)); }
@Test public void testQosFunctionForMeta() throws IOException { priority = regionServer.getPriority(); RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); //create a rpc request that has references to hbase:meta region and also //uses one of the known argument classes (known argument classes are //listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) headerBuilder.setMethodName("foo"); GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); ByteString name = HBaseZeroCopyByteString.wrap(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); regionSpecifierBuilder.setValue(name); RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); getRequestBuilder.setRegion(regionSpecifier); Get.Builder getBuilder = Get.newBuilder(); getBuilder.setRow(HBaseZeroCopyByteString.wrap("somerow".getBytes())); getRequestBuilder.setGet(getBuilder.build()); GetRequest getRequest = getRequestBuilder.build(); RequestHeader header = headerBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRS.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isMetaTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.HIGH_QOS, priority.getPriority(header, getRequest)); }
@Test public void testQosFunctionWithoutKnownArgument() throws IOException { //The request is not using any of the //known argument classes (it uses one random request class) //(known argument classes are listed in //HRegionServer.QosFunctionImpl.knownArgumentClasses) RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); headerBuilder.setMethodName("foo"); RequestHeader header = headerBuilder.build(); PriorityFunction qosFunc = regionServer.getPriority(); assertEquals(HConstants.NORMAL_QOS, qosFunc.getPriority(header, null)); }
/** * Initiates a call by sending the parameter to the remote server. * Note: this is not called from the Connection thread, but by other * threads. * @param call * @param priority * @see #readResponse() */ protected void writeRequest(Call call, final int priority) { if (shouldCloseConnection.get()) return; try { RequestHeader.Builder builder = RequestHeader.newBuilder(); builder.setCallId(call.id); if (Trace.isTracing()) { Span s = Trace.currentSpan(); builder.setTraceInfo(RPCTInfo.newBuilder(). setParentId(s.getSpanId()).setTraceId(s.getTraceId())); } builder.setMethodName(call.md.getName()); builder.setRequestParam(call.param != null); ByteBuffer cellBlock = ipcUtil.buildCellBlock(this.codec, this.compressor, call.cells); if (cellBlock != null) { CellBlockMeta.Builder cellBlockBuilder = CellBlockMeta.newBuilder(); cellBlockBuilder.setLength(cellBlock.limit()); builder.setCellBlockMeta(cellBlockBuilder.build()); } // Only pass priority if there one. Let zero be same as no priority. if (priority != 0) builder.setPriority(priority); //noinspection SynchronizeOnNonFinalField RequestHeader header = builder.build(); synchronized (this.out) { // FindBugs IS2_INCONSISTENT_SYNC IPCUtil.write(this.out, header, call.param, cellBlock); } if (LOG.isDebugEnabled()) { LOG.debug(getName() + ": wrote request header " + TextFormat.shortDebugString(header)); } } catch(IOException e) { markClosed(e); } }
@Test public void testQosFunctionForMeta() throws IOException { priority = regionServer.rpcServices.getPriority(); RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); //create a rpc request that has references to hbase:meta region and also //uses one of the known argument classes (known argument classes are //listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) headerBuilder.setMethodName("foo"); GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); ByteString name = HBaseZeroCopyByteString.wrap(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); regionSpecifierBuilder.setValue(name); RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); getRequestBuilder.setRegion(regionSpecifier); Get.Builder getBuilder = Get.newBuilder(); getBuilder.setRow(HBaseZeroCopyByteString.wrap("somerow".getBytes())); getRequestBuilder.setGet(getBuilder.build()); GetRequest getRequest = getRequestBuilder.build(); RequestHeader header = headerBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isMetaTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.HIGH_QOS, priority.getPriority(header, getRequest)); }
@Test public void testQosFunctionForMeta() throws IOException { qosFunction = regionServer.getQosFunction(); RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); //create a rpc request that has references to hbase:meta region and also //uses one of the known argument classes (known argument classes are //listed in HRegionServer.QosFunction.knownArgumentClasses) headerBuilder.setMethodName("foo"); GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); ByteString name = ZeroCopyLiteralByteString.wrap(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); regionSpecifierBuilder.setValue(name); RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); getRequestBuilder.setRegion(regionSpecifier); Get.Builder getBuilder = Get.newBuilder(); getBuilder.setRow(ZeroCopyLiteralByteString.wrap("somerow".getBytes())); getRequestBuilder.setGet(getBuilder.build()); GetRequest getRequest = getRequestBuilder.build(); RequestHeader header = headerBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRS.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isMetaTable()).thenReturn(true); qosFunction.setRegionServer(mockRS); assertTrue (qosFunction.apply(new Pair<RequestHeader, Message>(header, getRequest)) == HConstants.HIGH_QOS); }
@Test public void testQosFunctionWithoutKnownArgument() throws IOException { //The request is not using any of the //known argument classes (it uses one random request class) //(known argument classes are listed in //HRegionServer.QosFunction.knownArgumentClasses) RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); headerBuilder.setMethodName("foo"); RequestHeader header = headerBuilder.build(); QosFunction qosFunc = regionServer.getQosFunction(); assertTrue (qosFunc.apply(new Pair<RequestHeader, Message>(header, null)) == HConstants.NORMAL_QOS); }
private void checkMethod(final String methodName, final int expected, final QosFunction qosf) { RequestHeader.Builder builder = RequestHeader.newBuilder(); builder.setMethodName(methodName); Pair<RequestHeader, Message> headerAndParam = new Pair<RequestHeader, Message>(builder.build(), null); assertEquals(methodName, expected, qosf.apply(headerAndParam).intValue()); }
@Override public int getPriority(RequestHeader header, Message param, User user) { return priority.getPriority(header, param, user); }
@Override public long getDeadline(RequestHeader header, Message param) { return priority.getDeadline(header, param); }
protected RequestHeader getHeader() { return this.header; }
@Test public void testQosFunctionForScanMethod() throws IOException { RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); headerBuilder.setMethodName("Scan"); RequestHeader header = headerBuilder.build(); //build an empty scan request ScanRequest.Builder scanBuilder = ScanRequest.newBuilder(); ScanRequest scanRequest = scanBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(false); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); int qos = priority.getPriority(header, scanRequest, User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"})); assertTrue ("" + qos, qos == HConstants.NORMAL_QOS); //build a scan request with scannerID scanBuilder = ScanRequest.newBuilder(); scanBuilder.setScannerId(12345); scanRequest = scanBuilder.build(); //mock out a high priority type handling and see the QoS returned RegionScanner mockRegionScanner = Mockito.mock(RegionScanner.class); Mockito.when(mockRpc.getScanner(12345)).thenReturn(mockRegionScanner); Mockito.when(mockRegionScanner.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, scanRequest, User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"}))); //the same as above but with non-meta region Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(false); assertEquals(HConstants.NORMAL_QOS, priority.getPriority(header, scanRequest, User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"}))); }
private void testRpcScheduler(final String queueType) throws Exception { Configuration schedConf = HBaseConfiguration.create(); schedConf.set(SimpleRpcScheduler.CALL_QUEUE_TYPE_CONF_KEY, queueType); PriorityFunction priority = mock(PriorityFunction.class); when(priority.getPriority(any(RequestHeader.class), any(Message.class), any(User.class))) .thenReturn(HConstants.NORMAL_QOS); RpcScheduler scheduler = new SimpleRpcScheduler(schedConf, 1, 1, 1, priority, HConstants.QOS_THRESHOLD); try { scheduler.start(); CallRunner smallCallTask = mock(CallRunner.class); RpcServer.Call smallCall = mock(RpcServer.Call.class); RequestHeader smallHead = RequestHeader.newBuilder().setCallId(1).build(); when(smallCallTask.getCall()).thenReturn(smallCall); when(smallCall.getHeader()).thenReturn(smallHead); CallRunner largeCallTask = mock(CallRunner.class); RpcServer.Call largeCall = mock(RpcServer.Call.class); RequestHeader largeHead = RequestHeader.newBuilder().setCallId(50).build(); when(largeCallTask.getCall()).thenReturn(largeCall); when(largeCall.getHeader()).thenReturn(largeHead); CallRunner hugeCallTask = mock(CallRunner.class); RpcServer.Call hugeCall = mock(RpcServer.Call.class); RequestHeader hugeHead = RequestHeader.newBuilder().setCallId(100).build(); when(hugeCallTask.getCall()).thenReturn(hugeCall); when(hugeCall.getHeader()).thenReturn(hugeHead); when(priority.getDeadline(eq(smallHead), any(Message.class))).thenReturn(0L); when(priority.getDeadline(eq(largeHead), any(Message.class))).thenReturn(50L); when(priority.getDeadline(eq(hugeHead), any(Message.class))).thenReturn(100L); final ArrayList<Integer> work = new ArrayList<Integer>(); doAnswerTaskExecution(smallCallTask, work, 10, 250); doAnswerTaskExecution(largeCallTask, work, 50, 250); doAnswerTaskExecution(hugeCallTask, work, 100, 250); scheduler.dispatch(smallCallTask); scheduler.dispatch(smallCallTask); scheduler.dispatch(smallCallTask); scheduler.dispatch(hugeCallTask); scheduler.dispatch(smallCallTask); scheduler.dispatch(largeCallTask); scheduler.dispatch(smallCallTask); scheduler.dispatch(smallCallTask); while (work.size() < 8) { Threads.sleepWithoutInterrupt(100); } int seqSum = 0; int totalTime = 0; for (int i = 0; i < work.size(); ++i) { LOG.debug("Request i=" + i + " value=" + work.get(i)); seqSum += work.get(i); totalTime += seqSum; } LOG.debug("Total Time: " + totalTime); // -> [small small small huge small large small small] // -> NO REORDER [10 10 10 100 10 50 10 10] -> 930 (FIFO Queue) // -> WITH REORDER [10 10 10 10 10 10 50 100] -> 530 (Deadline Queue) if (queueType.equals(SimpleRpcScheduler.CALL_QUEUE_TYPE_DEADLINE_CONF_VALUE)) { assertEquals(530, totalTime); } else /* if (queueType.equals(SimpleRpcScheduler.CALL_QUEUE_TYPE_FIFO_CONF_VALUE)) */ { assertEquals(930, totalTime); } } finally { scheduler.stop(); } }
/** * Initiates a call by sending the parameter to the remote server. * Note: this is not called from the Connection thread, but by other * threads. * @see #readResponse() */ @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="IS2_INCONSISTENT_SYNC", justification="Findbugs is misinterpreting locking missing fact that this.outLock is held") private void writeRequest(Call call, final int priority, Span span) throws IOException { RequestHeader.Builder builder = RequestHeader.newBuilder(); builder.setCallId(call.id); if (span != null) { builder.setTraceInfo( RPCTInfo.newBuilder().setParentId(span.getSpanId()).setTraceId(span.getTraceId())); } builder.setMethodName(call.md.getName()); builder.setRequestParam(call.param != null); ByteBuffer cellBlock = ipcUtil.buildCellBlock(this.codec, this.compressor, call.cells); if (cellBlock != null) { CellBlockMeta.Builder cellBlockBuilder = CellBlockMeta.newBuilder(); cellBlockBuilder.setLength(cellBlock.limit()); builder.setCellBlockMeta(cellBlockBuilder.build()); } // Only pass priority if there one. Let zero be same as no priority. if (priority != 0) builder.setPriority(priority); RequestHeader header = builder.build(); setupIOstreams(); // Now we're going to write the call. We take the lock, then check that the connection // is still valid, and, if so we do the write to the socket. If the write fails, we don't // know where we stand, we have to close the connection. checkIsOpen(); IOException writeException = null; synchronized (this.outLock) { if (Thread.interrupted()) throw new InterruptedIOException(); calls.put(call.id, call); // We put first as we don't want the connection to become idle. checkIsOpen(); // Now we're checking that it didn't became idle in between. try { call.callStats.setRequestSizeBytes(IPCUtil.write(this.out, header, call.param, cellBlock)); } catch (IOException e) { // We set the value inside the synchronized block, this way the next in line // won't even try to write. Otherwise we might miss a call in the calls map? shouldCloseConnection.set(true); writeException = e; interrupt(); } } // call close outside of the synchronized (outLock) to prevent deadlock - HBASE-14474 if (writeException != null) { markClosed(writeException); close(); } // We added a call, and may be started the connection close. In both cases, we // need to notify the reader. doNotify(); // Now that we notified, we can rethrow the exception if any. Otherwise we're good. if (writeException != null) throw writeException; }
@Override public int getPriority(RequestHeader header, Message param) { return 0; }
@Override public int getPriority(RequestHeader header, Message param) { return priority.getPriority(header, param); }
@Test public void testQosFunctionForScanMethod() throws IOException { RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); headerBuilder.setMethodName("Scan"); RequestHeader header = headerBuilder.build(); //build an empty scan request ScanRequest.Builder scanBuilder = ScanRequest.newBuilder(); ScanRequest scanRequest = scanBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(false); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); int qos = priority.getPriority(header, scanRequest); assertTrue ("" + qos, qos == HConstants.NORMAL_QOS); //build a scan request with scannerID scanBuilder = ScanRequest.newBuilder(); scanBuilder.setScannerId(12345); scanRequest = scanBuilder.build(); //mock out a high priority type handling and see the QoS returned RegionScanner mockRegionScanner = Mockito.mock(RegionScanner.class); Mockito.when(mockRpc.getScanner(12345)).thenReturn(mockRegionScanner); Mockito.when(mockRegionScanner.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, scanRequest)); //the same as above but with non-meta region Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(false); assertEquals(HConstants.NORMAL_QOS, priority.getPriority(header, scanRequest)); }
private void checkMethod(final String methodName, final int expected, final AnnotationReadingPriorityFunction qosf, final Message param) { RequestHeader.Builder builder = RequestHeader.newBuilder(); builder.setMethodName(methodName); assertEquals(methodName, expected, qosf.getPriority(builder.build(), param)); }
private void testRpcScheduler(final String queueType) throws Exception { Configuration schedConf = HBaseConfiguration.create(); schedConf.set(SimpleRpcScheduler.CALL_QUEUE_TYPE_CONF_KEY, queueType); PriorityFunction priority = mock(PriorityFunction.class); when(priority.getPriority(any(RequestHeader.class), any(Message.class))) .thenReturn(HConstants.NORMAL_QOS); RpcScheduler scheduler = new SimpleRpcScheduler(schedConf, 1, 1, 1, priority, HConstants.QOS_THRESHOLD); try { scheduler.start(); CallRunner smallCallTask = mock(CallRunner.class); RpcServer.Call smallCall = mock(RpcServer.Call.class); RequestHeader smallHead = RequestHeader.newBuilder().setCallId(1).build(); when(smallCallTask.getCall()).thenReturn(smallCall); when(smallCall.getHeader()).thenReturn(smallHead); CallRunner largeCallTask = mock(CallRunner.class); RpcServer.Call largeCall = mock(RpcServer.Call.class); RequestHeader largeHead = RequestHeader.newBuilder().setCallId(50).build(); when(largeCallTask.getCall()).thenReturn(largeCall); when(largeCall.getHeader()).thenReturn(largeHead); CallRunner hugeCallTask = mock(CallRunner.class); RpcServer.Call hugeCall = mock(RpcServer.Call.class); RequestHeader hugeHead = RequestHeader.newBuilder().setCallId(100).build(); when(hugeCallTask.getCall()).thenReturn(hugeCall); when(hugeCall.getHeader()).thenReturn(hugeHead); when(priority.getDeadline(eq(smallHead), any(Message.class))).thenReturn(0L); when(priority.getDeadline(eq(largeHead), any(Message.class))).thenReturn(50L); when(priority.getDeadline(eq(hugeHead), any(Message.class))).thenReturn(100L); final ArrayList<Integer> work = new ArrayList<Integer>(); doAnswerTaskExecution(smallCallTask, work, 10, 250); doAnswerTaskExecution(largeCallTask, work, 50, 250); doAnswerTaskExecution(hugeCallTask, work, 100, 250); scheduler.dispatch(smallCallTask); scheduler.dispatch(smallCallTask); scheduler.dispatch(smallCallTask); scheduler.dispatch(hugeCallTask); scheduler.dispatch(smallCallTask); scheduler.dispatch(largeCallTask); scheduler.dispatch(smallCallTask); scheduler.dispatch(smallCallTask); while (work.size() < 8) { Threads.sleepWithoutInterrupt(100); } int seqSum = 0; int totalTime = 0; for (int i = 0; i < work.size(); ++i) { LOG.debug("Request i=" + i + " value=" + work.get(i)); seqSum += work.get(i); totalTime += seqSum; } LOG.debug("Total Time: " + totalTime); // -> [small small small huge small large small small] // -> NO REORDER [10 10 10 100 10 50 10 10] -> 930 (FIFO Queue) // -> WITH REORDER [10 10 10 10 10 10 50 100] -> 530 (Deadline Queue) if (queueType.equals(SimpleRpcScheduler.CALL_QUEUE_TYPE_DEADLINE_CONF_VALUE)) { assertEquals(530, totalTime); } else /* if (queueType.equals(SimpleRpcScheduler.CALL_QUEUE_TYPE_FIFO_CONF_VALUE)) */ { assertEquals(930, totalTime); } } finally { scheduler.stop(); } }