Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #273 from midwayjs/fix_bit_operation
Browse files Browse the repository at this point in the history
fix: use long replace number
  • Loading branch information
czy88840616 authored Oct 25, 2018
2 parents a28f503 + afca6d5 commit 290e988
Show file tree
Hide file tree
Showing 27 changed files with 710 additions and 156 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"packages": [
"packages/*"
],
"version": "1.10.0",
"version": "1.10.1",
"registry": "https://registry.npmjs.org/",
"npmClient": "npm"
}
2 changes: 1 addition & 1 deletion packages/dollar/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pandora-dollar",
"version": "1.10.0",
"version": "1.10.1",
"description": "handy utilities",
"main": "dist/index",
"typings": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/env/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pandora-env",
"version": "1.10.0",
"version": "1.10.1",
"description": "Common environment abstract",
"main": "dist/index",
"typings": "dist/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/hook/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pandora-hook",
"version": "1.10.0",
"version": "1.10.1",
"description": "pandora module hook",
"main": "dist/index",
"typings": "dist/index.d.ts",
Expand All @@ -25,7 +25,7 @@
"cls-bluebird": "^2.1.0",
"debug": "^3.1.0",
"is-type-of": "^1.2.0",
"pandora-metrics": "^1.10.0",
"pandora-metrics": "^1.10.1",
"semver": "^5.4.1",
"xorshift": "^1.1.1"
},
Expand All @@ -50,7 +50,7 @@
"nock": "^9.1.4",
"nyc": "^11.3.0",
"nyc-ts-patch": "^1.0.0",
"pandora-env": "^1.10.0",
"pandora-env": "^1.10.1",
"pedding": "^1.1.0",
"redis-parser": "^3.0.0",
"safe-buffer": "^5.1.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/hub/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pandora-hub",
"version": "1.10.0",
"version": "1.10.1",
"description": "pandora.js messenge hub",
"main": "dist/index",
"typings": "dist/index.d.ts",
Expand All @@ -21,7 +21,7 @@
],
"license": "MIT",
"dependencies": {
"pandora-messenger": "^1.10.0",
"pandora-messenger": "^1.10.1",
"uuid": "^3.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/messenger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pandora-messenger",
"version": "1.10.0",
"version": "1.10.1",
"description": "pandora.js messenger module",
"main": "dist/index",
"typings": "dist/index.d.ts",
Expand Down
11 changes: 6 additions & 5 deletions packages/metrics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pandora-metrics",
"version": "1.10.0",
"version": "1.10.1",
"main": "dist/index",
"typings": "dist/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -31,7 +31,7 @@
"mm": "^2.1.0",
"mocha": "^3.5.0",
"nyc": "^11.2.1",
"pandora-hub": "^1.10.0",
"pandora-hub": "^1.10.1",
"sinon": "^4.1.2",
"supertest": "^3.0.0",
"yargs": "^6.6.0"
Expand All @@ -45,13 +45,14 @@
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"koa-router": "^7.2.1",
"long": "^4.0.0",
"mixin": "^0.2.0",
"module-hook": "^2.1.0",
"node-df": "^0.1.1",
"opentracing": "^0.14.1",
"pandora-env": "^1.10.0",
"pandora-messenger": "^1.10.0",
"pandora-service-logger": "^1.10.0",
"pandora-env": "^1.10.1",
"pandora-messenger": "^1.10.1",
"pandora-service-logger": "^1.10.1",
"pidusage": "^1.1.6",
"xorshift": "^1.1.1"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/metrics/src/MetricsServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ export class MetricsServerManager extends AbstractIndicator implements MetricsMa
result.set(MetricType.HISTOGRAM, metricRegistry.getHistograms(filter));
result.set(MetricType.METER, metricRegistry.getMeters(filter));
result.set(MetricType.TIMER, metricRegistry.getTimers(filter));
result.set(MetricType.FASTCOMPASS, metricRegistry.getFastCompasses(filter));

return result;
}
Expand All @@ -350,6 +351,7 @@ export class MetricsServerManager extends AbstractIndicator implements MetricsMa
result.set(MetricType.HISTOGRAM, allMetricsRegistry.getHistograms(filter));
result.set(MetricType.METER, allMetricsRegistry.getMeters(filter));
result.set(MetricType.TIMER, allMetricsRegistry.getTimers(filter));
result.set(MetricType.FASTCOMPASS, allMetricsRegistry.getFastCompasses(filter));

return result;
}
Expand Down
45 changes: 21 additions & 24 deletions packages/metrics/src/collect/CompactMetricsCollector.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {MetricsCollector} from './MetricsCollector';
import { BucketCounter, IFastCompass, MetricName } from '../common/index';
import {MetricObject} from './MetricObject';
import {ITimer} from '../common/metrics/Timer';
import {IHistogram} from '../common/metrics/Histogram';
import {ICounter} from '../common/metrics/Counter';
import {IMeter} from '../common/metrics/Meter';
import {Snapshot} from '../common/domain';
import { MetricsCollector } from './MetricsCollector';
import { BucketCounter, ICounter, IFastCompass, IHistogram, IMeter, ITimer, MetricName, Snapshot } from '../common';
import { MetricObject } from './MetricObject';

const BigNumber = require('long');
/**
* 根据采集周期获取采集点,支持多个采集点的输出
*/
Expand All @@ -17,7 +13,7 @@ export class CompactMetricsCollector extends MetricsCollector {
let startTime = timestamp - this.reportInterval * 1000 + 1;

let totalCounts = timer.getInstantCount(startTime);
for(let [time, metricValue] of totalCounts.entries()) {
for (let [ time, metricValue ] of totalCounts.entries()) {
this.addMetricWithSuffix(name, 'bucket_count', metricValue, time, MetricObject.MetricType.DELTA, this.metricsCollectPeriodConfig.period(name.getMetricLevel()));
}

Expand Down Expand Up @@ -46,7 +42,7 @@ export class CompactMetricsCollector extends MetricsCollector {

if (counter instanceof BucketCounter) {
let totalCounts = (<BucketCounter> counter).getBucketCounts(startTime);
for(let [time, metricValue] of totalCounts.entries()) {
for (let [ time, metricValue ] of totalCounts.entries()) {
this.addMetricWithSuffix(name, 'bucket_count', metricValue, time, MetricObject.MetricType.DELTA, this.metricsCollectPeriodConfig.period(name.getMetricLevel()));
}
}
Expand All @@ -60,7 +56,7 @@ export class CompactMetricsCollector extends MetricsCollector {
let startTime = timestamp - this.reportInterval * 1000 + 1;
let totalCounts = meter.getInstantCount(startTime);

for(let [time, metricValue] of totalCounts.entries()) {
for (let [ time, metricValue ] of totalCounts.entries()) {
this.addMetricWithSuffix(name, 'bucket_count', metricValue, time, MetricObject.MetricType.DELTA, this.metricsCollectPeriodConfig.period(name.getMetricLevel()));
}

Expand All @@ -73,23 +69,24 @@ export class CompactMetricsCollector extends MetricsCollector {
let bucketInterval = fastCompass.getBucketInterval();

let start = this.getNormalizedStartTime(timestamp, bucketInterval);
let totalCount = 0;
let totalRt = 0;
let successCount = 0;
let hitCount = -1;
let totalCount = new BigNumber();
let totalRt = new BigNumber();
let successCount = new BigNumber();
let hitCount = new BigNumber(-1, -1);

let countPerCategory = fastCompass.getMethodCountPerCategory(start);
for (let [key, value] of countPerCategory.entries()) {
for (let [ key, value ] of countPerCategory.entries()) {
if (value.has(start)) {
this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start), start,
this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start).toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
totalCount += value.get(start);

totalCount = totalCount.add(value.get(start));
if ('success' === key) {
successCount += value.get(start);
successCount = successCount.add(value.get(start));
}
if ('hit' === key) {
hitCount = value.get(start);
successCount += value.get(start);
successCount = successCount.add(value.get(start));
}
} else {
this.addMetricWithSuffix(name, key + '_bucket_count', 0, start,
Expand All @@ -99,20 +96,20 @@ export class CompactMetricsCollector extends MetricsCollector {

for (let value of fastCompass.getMethodRtPerCategory(start).values()) {
if (value.has(start)) {
totalRt += value.get(start);
totalRt = totalRt.add(value.get(start));
}
}
this.addMetricWithSuffix(name, 'bucket_count', totalCount, start,
this.addMetricWithSuffix(name, 'bucket_count', totalCount.toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
this.addMetricWithSuffix(name, 'bucket_sum', totalRt, start,
this.addMetricWithSuffix(name, 'bucket_sum', totalRt.toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
this.addMetricWithSuffix(name, 'qps', this.rate(totalCount, bucketInterval), start,
MetricObject.MetricType.GAUGE, bucketInterval);
this.addMetricWithSuffix(name, 'rt', this.rate(totalRt, totalCount), start,
MetricObject.MetricType.GAUGE, bucketInterval);
this.addMetricWithSuffix(name, 'success_rate', this.ratio(successCount, totalCount), start,
MetricObject.MetricType.GAUGE, bucketInterval);
if (hitCount >= 0) {
if (hitCount.gte(0)) {
this.addMetricWithSuffix(name, 'hit_rate', this.ratio(hitCount, successCount), start,
MetricObject.MetricType.GAUGE, bucketInterval);
}
Expand Down
27 changes: 20 additions & 7 deletions packages/metrics/src/collect/MetricsCollector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {MetricFilter, MetricName, MetricsCollectPeriodConfig} from '../common/index';
import {CollectMetricType, MetricObject} from './MetricObject';
import { MetricFilter, MetricName, MetricsCollectPeriodConfig } from '../common/index';
import { CollectMetricType, MetricObject } from './MetricObject';
import { Long } from '../domain';

export class MetricsCollector {
protected metrics: Array<MetricObject> = [];
Expand All @@ -13,7 +14,7 @@ export class MetricsCollector {
*/
protected filter: MetricFilter;

constructor(options: { globalTags, rateFactor, durationFactor, filter?, reportInterval?}) {
constructor(options: { globalTags, rateFactor, durationFactor, filter?, reportInterval? }) {
this.globalTags = options.globalTags;
this.rateFactor = options.rateFactor;
this.durationFactor = options.durationFactor;
Expand Down Expand Up @@ -77,12 +78,24 @@ export class MetricsCollector {

public rate(data, interval) {
if (interval === 0) return 0.0;
return data / interval;
if (typeof interval !== 'number' && interval.isZero()) return 0.0;

if (typeof data !== 'number') {
return <Long>data.div(interval).toString();
} else {
return data / interval;
}
}

public ratio(data, total) {
if (data > total) return 1.0;
if (total === 0) return 0.0;
return 1.0 * data / total;
if (typeof data !== 'number') {
if (<Long>data.gt(total)) return 1.0;
if (<Long>total.eq(0)) return 0.0;
return <Long>data.div(total).toString();
} else {
if (data > total) return 1.0;
if (total === 0) return 0.0;
return 1.0 * data / total;
}
}
}
27 changes: 15 additions & 12 deletions packages/metrics/src/collect/NormalMetricsCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { MetricsCollector } from './MetricsCollector';
import { BucketCounter, ICounter, IFastCompass, IHistogram, IMeter, ITimer, MetricName, Snapshot } from '../common';
import { MetricObject } from './MetricObject';

const BigNumber = require('long');

export class NormalMetricsCollector extends MetricsCollector {

collectTimer(name: MetricName, timer: ITimer, timestamp: number) {
Expand Down Expand Up @@ -68,23 +70,24 @@ export class NormalMetricsCollector extends MetricsCollector {
let bucketInterval = fastCompass.getBucketInterval();

let start = this.getNormalizedStartTime(timestamp, bucketInterval);
let totalCount = 0;
let totalRt = 0;
let successCount = 0;
let hitCount = -1;
let totalCount = new BigNumber();
let totalRt = new BigNumber();
let successCount = new BigNumber();
let hitCount = new BigNumber(-1, -1);

let countPerCategory = fastCompass.getMethodCountPerCategory(start);
for (let [ key, value ] of countPerCategory.entries()) {
if (value.has(start)) {
this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start), start,
this.addMetricWithSuffix(name, key + '_bucket_count', value.get(start).toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
totalCount += value.get(start);

totalCount = totalCount.add(value.get(start));
if ('success' === key) {
successCount += value.get(start);
successCount = successCount.add(value.get(start));
}
if ('hit' === key) {
hitCount = value.get(start);
successCount += value.get(start);
successCount = successCount.add(value.get(start));
}
} else {
this.addMetricWithSuffix(name, key + '_bucket_count', 0, start,
Expand All @@ -94,20 +97,20 @@ export class NormalMetricsCollector extends MetricsCollector {

for (let value of fastCompass.getMethodRtPerCategory(start).values()) {
if (value.has(start)) {
totalRt += value.get(start);
totalRt = totalRt.add(value.get(start));
}
}
this.addMetricWithSuffix(name, 'bucket_count', totalCount, start,
this.addMetricWithSuffix(name, 'bucket_count', totalCount.toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
this.addMetricWithSuffix(name, 'bucket_sum', totalRt, start,
this.addMetricWithSuffix(name, 'bucket_sum', totalRt.toString(), start,
MetricObject.MetricType.DELTA, bucketInterval);
this.addMetricWithSuffix(name, 'qps', this.rate(totalCount, bucketInterval), start,
MetricObject.MetricType.GAUGE, bucketInterval);
this.addMetricWithSuffix(name, 'rt', this.rate(totalRt, totalCount), start,
MetricObject.MetricType.GAUGE, bucketInterval);
this.addMetricWithSuffix(name, 'success_rate', this.ratio(successCount, totalCount), start,
MetricObject.MetricType.GAUGE, bucketInterval);
if (hitCount >= 0) {
if (hitCount.gte(0)) {
this.addMetricWithSuffix(name, 'hit_rate', this.ratio(hitCount, successCount), start,
MetricObject.MetricType.GAUGE, bucketInterval);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/src/common/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Counting {
*
* @return the current count
*/
getCount(): number;
getCount(): number | string;
}


Expand Down
Loading

0 comments on commit 290e988

Please sign in to comment.