首页 > 代码库 > 元注解:@Retention 在hbase中用于获取确定高优先级的RPC操作
元注解:@Retention 在hbase中用于获取确定高优先级的RPC操作
概念
注解@Retention可以用来修饰注解,是注解的注解,称为元注解。
Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚举类型,
这个枚举决定了Retention注解应该如何去保持,也可理解为Rentention 搭配 RententionPolicy使用。
RetentionPolicy有3个值:CLASS RUNTIME SOURCE
用@Retention(RetentionPolicy.CLASS)修饰的注解,表示注解的信息被保留在class文件(字节码文件)中当程序编译时,但不会被虚拟机读取在运行的时候;
用@Retention(RetentionPolicy.SOURCE )修饰的注解,表示注解的信息会被编译器抛弃,不会留在class文件中,注解的信息只会留在源文件中;
用@Retention(RetentionPolicy.RUNTIME )修饰的注解,表示注解的信息被保留在class文件(字节码文件)中当程序编译时,会被虚拟机保留在运行时,
所以他们可以用反射的方式读取。RetentionPolicy.RUNTIME 可以让你从JVM中读取Annotation注解的信息,以便在分析程序的时候使用.
在hbase中的使用
参见类org.apache.hadoop.hbase.regionserver.AnnotationReadingPriorityFunction
用于确定高优先级的RPC操作
1:如果请求的方法使用了QosPriority of QOS_HIGH注释,则认为是高优先级
2:如果请求是操作meta region,则认为是高优先级(代码层面针对此做了一些优化操作,避免过早的反序列化PB对象)
代码示例
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; import java.net.UnknownHostException; import java.util.HashMap; import java.util.Map; import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CloseRegionResponse; import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CompactRegionResponse; import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetOnlineRegionResponse; import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetRegionInfoResponse; import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.MergeRegionsResponse; import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.OpenRegionResponse; import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.ReplicateWALEntryResponse; import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.SplitRegionResponse; @SuppressWarnings("unchecked") public class Test { public static void main(String[] args) throws UnknownHostException { Test test = new Test(); } public Test() { Map<String, Integer> qosMap = new HashMap<String, Integer>(); for (Method m : Test.class.getMethods()) { QosPriority p = m.getAnnotation(QosPriority.class); if (p != null) { String capitalizedMethodName = capitalize(m.getName()); qosMap.put(capitalizedMethodName, p.priority()); System.out.println(capitalizedMethodName + "," + p.priority()); } } } @Retention(RetentionPolicy.RUNTIME) protected @interface QosPriority { int priority() default 0; } @QosPriority(priority = HConstants.HIGH_QOS) public GetRegionInfoResponse getRegionInfo() { System.out.println("getRegionInfo"); return null; } @QosPriority(priority = HConstants.HIGH_QOS) public GetOnlineRegionResponse getOnlineRegion() { System.out.println("getOnlineRegion"); return null; } @QosPriority(priority = HConstants.HIGH_QOS) public OpenRegionResponse openRegion() { System.out.println("openRegion"); return null; } @QosPriority(priority = HConstants.HIGH_QOS) public CloseRegionResponse closeRegion() { System.out.println("closeRegion"); return null; } @QosPriority(priority = HConstants.HIGH_QOS) public SplitRegionResponse splitRegion() { System.out.println("splitRegion"); return null; } @QosPriority(priority = HConstants.HIGH_QOS) public MergeRegionsResponse mergeRegions() { System.out.println("mergeRegions"); return null; } @QosPriority(priority = HConstants.HIGH_QOS) public CompactRegionResponse compactRegion() { System.out.println("compactRegion"); return null; } @QosPriority(priority = HConstants.REPLICATION_QOS) public ReplicateWALEntryResponse replicateWALEntry() { System.out.println("replicateWALEntry"); return null; } @QosPriority(priority = HConstants.REPLAY_QOS) public ReplicateWALEntryResponse replay() { System.out.println("replay"); return null; } private String capitalize(final String s) { StringBuilder strBuilder = new StringBuilder(s); strBuilder.setCharAt(0, Character.toUpperCase(strBuilder.charAt(0))); return strBuilder.toString(); } }
元注解:@Retention 在hbase中用于获取确定高优先级的RPC操作