-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
297 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/javagrunt/service/gateway/GrpcServerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.javagrunt.service.gateway; | ||
|
||
import externalscaler.ExternalScalerGrpc.ExternalScalerImplBase; | ||
import externalscaler.Externalscaler.*; | ||
import io.grpc.stub.StreamObserver; | ||
import net.devh.boot.grpc.server.service.GrpcService; | ||
import org.springframework.scheduling.annotation.Async; | ||
|
||
@GrpcService | ||
public class GrpcServerService extends ExternalScalerImplBase { | ||
|
||
private final ScalerService scaler; | ||
|
||
GrpcServerService(ScalerService scaler) { | ||
this.scaler = scaler; | ||
} | ||
|
||
@Override | ||
public void isActive(ScaledObjectRef request, StreamObserver<IsActiveResponse> responseObserver) { | ||
responseObserver.onNext(IsActiveResponse.newBuilder().setResult(scaler.isActive(request.getName())).build()); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
@Override | ||
@Async | ||
public void streamIsActive(ScaledObjectRef request, StreamObserver<IsActiveResponse> responseObserver) { | ||
boolean active = scaler.isActive(request.getName()); | ||
responseObserver.onNext(IsActiveResponse.newBuilder().setResult(active).build()); | ||
while (true) { | ||
try { | ||
Thread.sleep(1000); | ||
} | ||
catch (InterruptedException e) { | ||
responseObserver.onError(e); | ||
Thread.currentThread().interrupt(); | ||
} | ||
boolean update = scaler.isActive(request.getName()); | ||
if (update != active) { | ||
active = update; | ||
responseObserver.onNext(IsActiveResponse.newBuilder().setResult(active).build()); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void getMetricSpec(ScaledObjectRef request, StreamObserver<GetMetricSpecResponse> responseObserver) { | ||
responseObserver.onNext(GetMetricSpecResponse.newBuilder() | ||
.addMetricSpecs(MetricSpec.newBuilder() | ||
.setMetricName("requests") | ||
.setTargetSize(Integer.parseInt(request.getScalerMetadataOrDefault("threshold", "3"))) | ||
.build()) | ||
.build()); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
@Override | ||
public void getMetrics(GetMetricsRequest request, StreamObserver<GetMetricsResponse> responseObserver) { | ||
responseObserver.onNext(GetMetricsResponse.newBuilder() | ||
.addMetricValues(MetricValue.newBuilder() | ||
.setMetricName("requests") | ||
.setMetricValue(scaler.getMetric(request.getScaledObjectRef().getName())) | ||
.build()) | ||
.build()); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/javagrunt/service/gateway/ScalerEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.javagrunt.service.gateway; | ||
|
||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; | ||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; | ||
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Endpoint(id = "scaler") | ||
public class ScalerEndpoint { | ||
|
||
private final ScalerService scaler; | ||
|
||
ScalerEndpoint(ScalerService scaler) { | ||
this.scaler = scaler; | ||
} | ||
|
||
@ReadOperation | ||
public boolean status(String name) { | ||
return scaler.isActive(name); | ||
} | ||
|
||
@WriteOperation | ||
public void toggle(String name) { | ||
scaler.setActive(name, !scaler.isActive(name)); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/javagrunt/service/gateway/ScalerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.javagrunt.service.gateway; | ||
|
||
public interface ScalerService { | ||
|
||
boolean isActive(String name); | ||
|
||
void setActive(String name, boolean active); | ||
|
||
long getMetric(String name); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
syntax = "proto3"; | ||
|
||
package externalscaler; | ||
option go_package = ".;externalscaler"; | ||
|
||
service ExternalScaler { | ||
rpc IsActive(ScaledObjectRef) returns (IsActiveResponse) {} | ||
rpc StreamIsActive(ScaledObjectRef) returns (stream IsActiveResponse) {} | ||
rpc GetMetricSpec(ScaledObjectRef) returns (GetMetricSpecResponse) {} | ||
rpc GetMetrics(GetMetricsRequest) returns (GetMetricsResponse) {} | ||
} | ||
|
||
message ScaledObjectRef { | ||
string name = 1; | ||
string namespace = 2; | ||
map<string, string> scalerMetadata = 3; | ||
} | ||
|
||
message IsActiveResponse { | ||
bool result = 1; | ||
} | ||
|
||
message GetMetricSpecResponse { | ||
repeated MetricSpec metricSpecs = 1; | ||
} | ||
|
||
message MetricSpec { | ||
string metricName = 1; | ||
int64 targetSize = 2; | ||
} | ||
|
||
message GetMetricsRequest { | ||
ScaledObjectRef scaledObjectRef = 1; | ||
string metricName = 2; | ||
} | ||
|
||
message GetMetricsResponse { | ||
repeated MetricValue metricValues = 1; | ||
} | ||
|
||
message MetricValue { | ||
string metricName = 1; | ||
int64 metricValue = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters