|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.kyuubi.server |
| 19 | + |
| 20 | +import org.apache.hive.service.rpc.thrift._ |
| 21 | + |
| 22 | +import org.apache.kyuubi.metrics.{MetricsConstants, MetricsSystem} |
| 23 | +import org.apache.kyuubi.operation.{OperationHandle, OperationStatus} |
| 24 | +import org.apache.kyuubi.operation.FetchOrientation.FetchOrientation |
| 25 | +import org.apache.kyuubi.service.BackendService |
| 26 | +import org.apache.kyuubi.session.SessionHandle |
| 27 | + |
| 28 | +trait BackendServiceTimeMetric extends BackendService { |
| 29 | + |
| 30 | + @throws[Exception] |
| 31 | + private def timeMetric[T](name: String)(f: => T): T = { |
| 32 | + val startTime = System.currentTimeMillis() |
| 33 | + try { |
| 34 | + f |
| 35 | + } finally { |
| 36 | + MetricsSystem.tracing( |
| 37 | + _.updateHistogram(name, System.currentTimeMillis() - startTime)) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + abstract override def openSession( |
| 42 | + protocol: TProtocolVersion, |
| 43 | + user: String, |
| 44 | + password: String, |
| 45 | + ipAddr: String, |
| 46 | + configs: Map[String, String]): SessionHandle = { |
| 47 | + timeMetric(MetricsConstants.OPEN_SESSION_MS) { |
| 48 | + super.openSession(protocol, user, password, ipAddr, configs) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + abstract override def closeSession(sessionHandle: SessionHandle): Unit = { |
| 53 | + timeMetric(MetricsConstants.CLOSE_SESSION_MS) { |
| 54 | + super.closeSession(sessionHandle) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + abstract override def getInfo( |
| 59 | + sessionHandle: SessionHandle, |
| 60 | + infoType: TGetInfoType): TGetInfoValue = { |
| 61 | + timeMetric(MetricsConstants.GET_INFO_MS) { |
| 62 | + super.getInfo(sessionHandle, infoType) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + abstract override def executeStatement( |
| 67 | + sessionHandle: SessionHandle, |
| 68 | + statement: String, |
| 69 | + runAsync: Boolean, |
| 70 | + queryTimeout: Long): OperationHandle = { |
| 71 | + timeMetric(MetricsConstants.EXECUTE_STATEMENT_MS) { |
| 72 | + super.executeStatement(sessionHandle, statement, runAsync, queryTimeout) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + abstract override def getTypeInfo(sessionHandle: SessionHandle): OperationHandle = { |
| 77 | + timeMetric(MetricsConstants.GET_TYPE_INFO_MS) { |
| 78 | + super.getTypeInfo(sessionHandle) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + abstract override def getCatalogs(sessionHandle: SessionHandle): OperationHandle = { |
| 83 | + timeMetric(MetricsConstants.GET_CATALOGS_MS) { |
| 84 | + super.getCatalogs(sessionHandle) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + abstract override def getSchemas( |
| 89 | + sessionHandle: SessionHandle, |
| 90 | + catalogName: String, |
| 91 | + schemaName: String): OperationHandle = { |
| 92 | + timeMetric(MetricsConstants.GET_SCHEMAS_MS) { |
| 93 | + super.getSchemas(sessionHandle, catalogName, schemaName) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + abstract override def getTables( |
| 98 | + sessionHandle: SessionHandle, |
| 99 | + catalogName: String, |
| 100 | + schemaName: String, |
| 101 | + tableName: String, |
| 102 | + tableTypes: java.util.List[String]): OperationHandle = { |
| 103 | + timeMetric(MetricsConstants.GET_TABLES_MS) { |
| 104 | + super.getTables(sessionHandle, catalogName, schemaName, tableName, tableTypes) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + abstract override def getTableTypes(sessionHandle: SessionHandle): OperationHandle = { |
| 109 | + timeMetric(MetricsConstants.GET_TABLE_TYPES_MS) { |
| 110 | + super.getTableTypes(sessionHandle) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + abstract override def getColumns( |
| 115 | + sessionHandle: SessionHandle, |
| 116 | + catalogName: String, |
| 117 | + schemaName: String, |
| 118 | + tableName: String, |
| 119 | + columnName: String): OperationHandle = { |
| 120 | + timeMetric(MetricsConstants.GET_COLUMNS_MS) { |
| 121 | + super.getColumns(sessionHandle, catalogName, schemaName, tableName, columnName) |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + abstract override def getFunctions( |
| 126 | + sessionHandle: SessionHandle, |
| 127 | + catalogName: String, |
| 128 | + schemaName: String, |
| 129 | + functionName: String): OperationHandle = { |
| 130 | + timeMetric(MetricsConstants.GET_FUNCTIONS_MS) { |
| 131 | + super.getFunctions(sessionHandle, catalogName, schemaName, functionName) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + abstract override def getOperationStatus(operationHandle: OperationHandle): OperationStatus = { |
| 136 | + timeMetric(MetricsConstants.GET_OPERATION_STATUS_MS) { |
| 137 | + super.getOperationStatus(operationHandle) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + abstract override def cancelOperation(operationHandle: OperationHandle): Unit = { |
| 142 | + timeMetric(MetricsConstants.CANCEL_OPERATION_MS) { |
| 143 | + super.cancelOperation(operationHandle) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + abstract override def closeOperation(operationHandle: OperationHandle): Unit = { |
| 148 | + timeMetric(MetricsConstants.CLOSE_OPERATION_MS) { |
| 149 | + super.closeOperation(operationHandle) |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + abstract override def getResultSetMetadata(operationHandle: OperationHandle): TTableSchema = { |
| 154 | + timeMetric(MetricsConstants.GET_RESULT_SET_METADATA_MS) { |
| 155 | + super.getResultSetMetadata(operationHandle) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + abstract override def fetchResults( |
| 160 | + operationHandle: OperationHandle, |
| 161 | + orientation: FetchOrientation, |
| 162 | + maxRows: Int, |
| 163 | + fetchLog: Boolean): TRowSet = { |
| 164 | + timeMetric(MetricsConstants.FETCH_RESULTS_MS) { |
| 165 | + super.fetchResults(operationHandle, orientation, maxRows, fetchLog) |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | +} |
0 commit comments