ServerStats.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2008, Yahoo! Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.yahoo.zookeeper.server;
  17. public class ServerStats {
  18. private static ServerStats instance=null;
  19. private long packetsSent;
  20. private long packetsReceived;
  21. private long maxLatency;
  22. private long minLatency = Long.MAX_VALUE;
  23. private long totalLatency = 0;
  24. private long count = 0;
  25. public interface Provider{
  26. public long getOutstandingRequests();
  27. public long getLastProcessedZxid();
  28. }
  29. private Provider provider=null;
  30. private Object mutex=new Object();
  31. static public ServerStats getInstance(){
  32. return instance;
  33. }
  34. static public void registerAsConcrete() {
  35. setInstance(new ServerStats());
  36. }
  37. static synchronized public void unregister() {
  38. instance=null;
  39. }
  40. static synchronized protected void setInstance(ServerStats newInstance){
  41. assert instance==null;
  42. instance = newInstance;
  43. }
  44. protected ServerStats(){}
  45. // getters
  46. synchronized public long getMinLatency() {
  47. return (minLatency == Long.MAX_VALUE) ? 0 : minLatency;
  48. }
  49. synchronized public long getAvgLatency() {
  50. if(count!=0)
  51. return totalLatency / count;
  52. return 0;
  53. }
  54. synchronized public long getMaxLatency() {
  55. return maxLatency;
  56. }
  57. public long getOutstandingRequests() {
  58. synchronized(mutex){
  59. return (provider!=null)?provider.getOutstandingRequests():-1;
  60. }
  61. }
  62. public long getLastProcessedZxid(){
  63. synchronized(mutex){
  64. return (provider!=null)?provider.getLastProcessedZxid():-1;
  65. }
  66. }
  67. synchronized public long getPacketsReceived() {
  68. return packetsReceived;
  69. }
  70. synchronized public long getPacketsSent() {
  71. return packetsSent;
  72. }
  73. public String getServerState(){
  74. return "standalone";
  75. }
  76. public String toString(){
  77. StringBuilder sb = new StringBuilder();
  78. sb.append("Latency min/avg/max: " + getMinLatency() + "/"
  79. + getAvgLatency() + "/" + getMaxLatency() + "\n");
  80. sb.append("Received: " + getPacketsReceived() + "\n");
  81. sb.append("Sent: " + getPacketsSent() + "\n");
  82. if (provider != null) {
  83. sb.append("Outstanding: " + getOutstandingRequests() + "\n");
  84. sb.append("Zxid: 0x"+ Long.toHexString(getLastProcessedZxid())+ "\n");
  85. }
  86. sb.append("Mode: "+getServerState()+"\n");
  87. return sb.toString();
  88. }
  89. // mutators
  90. public void setStatsProvider(Provider zk){
  91. synchronized(mutex){
  92. provider=zk;
  93. }
  94. }
  95. synchronized void updateLatency(long requestCreateTime) {
  96. long latency = System.currentTimeMillis() - requestCreateTime;
  97. totalLatency += latency;
  98. count++;
  99. if (latency < minLatency) {
  100. minLatency = latency;
  101. }
  102. if (latency > maxLatency) {
  103. maxLatency = latency;
  104. }
  105. }
  106. synchronized public void resetLatency(){
  107. totalLatency=count=maxLatency=0;
  108. minLatency=Long.MAX_VALUE;
  109. }
  110. synchronized public void resetMaxLatency(){
  111. maxLatency=getMinLatency();
  112. }
  113. synchronized public void incrementPacketsReceived() {
  114. packetsReceived++;
  115. }
  116. synchronized public void incrementPacketsSent() {
  117. packetsSent++;
  118. }
  119. synchronized public void resetRequestCounters(){
  120. packetsReceived=packetsSent=0;
  121. }
  122. }