ShardQueryCache.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Licensed to Elasticsearch under one or more contributor
  3. * license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright
  5. * ownership. Elasticsearch licenses this file to you under
  6. * the Apache License, Version 2.0 (the "License"); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.elasticsearch.index.cache.query;
  20. import com.google.common.cache.RemovalListener;
  21. import com.google.common.cache.RemovalNotification;
  22. import org.elasticsearch.common.inject.Inject;
  23. import org.elasticsearch.common.metrics.CounterMetric;
  24. import org.elasticsearch.common.settings.Settings;
  25. import org.elasticsearch.index.settings.IndexSettings;
  26. import org.elasticsearch.index.shard.AbstractIndexShardComponent;
  27. import org.elasticsearch.index.shard.ShardId;
  28. import org.elasticsearch.indices.cache.query.IndicesQueryCache;
  29. /**
  30. */
  31. public class ShardQueryCache extends AbstractIndexShardComponent implements RemovalListener<IndicesQueryCache.Key, IndicesQueryCache.Value> {
  32. final CounterMetric evictionsMetric = new CounterMetric();
  33. final CounterMetric totalMetric = new CounterMetric();
  34. final CounterMetric hitCount = new CounterMetric();
  35. final CounterMetric missCount = new CounterMetric();
  36. @Inject
  37. public ShardQueryCache(ShardId shardId, @IndexSettings Settings indexSettings) {
  38. super(shardId, indexSettings);
  39. }
  40. public QueryCacheStats stats() {
  41. return new QueryCacheStats(totalMetric.count(), evictionsMetric.count(), hitCount.count(), missCount.count());
  42. }
  43. public void onHit() {
  44. hitCount.inc();
  45. }
  46. public void onMiss() {
  47. missCount.inc();
  48. }
  49. public void onCached(IndicesQueryCache.Key key, IndicesQueryCache.Value value) {
  50. totalMetric.inc(key.ramBytesUsed() + value.ramBytesUsed());
  51. }
  52. @Override
  53. public void onRemoval(RemovalNotification<IndicesQueryCache.Key, IndicesQueryCache.Value> removalNotification) {
  54. if (removalNotification.wasEvicted()) {
  55. evictionsMetric.inc();
  56. }
  57. long dec = 0;
  58. if (removalNotification.getKey() != null) {
  59. dec += removalNotification.getKey().ramBytesUsed();
  60. }
  61. if (removalNotification.getValue() != null) {
  62. dec += removalNotification.getValue().ramBytesUsed();
  63. }
  64. totalMetric.dec(dec);
  65. }
  66. }