-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw 503 error if all workers are busy
Implement #341 Change-Id: I7770744d50bb21047c0b66e0d255d49a6d33e672
- Loading branch information
Showing
6 changed files
with
213 additions
and
17 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadDetectFilter.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,58 @@ | ||
/* | ||
* Copyright 2017 HugeGraph Authors | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.baidu.hugegraph.api.filter; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.inject.Singleton; | ||
import javax.ws.rs.ServiceUnavailableException; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import javax.ws.rs.container.PreMatching; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import com.baidu.hugegraph.config.HugeConfig; | ||
import com.baidu.hugegraph.config.ServerOptions; | ||
import com.baidu.hugegraph.core.WorkLoad; | ||
|
||
@Provider | ||
@Singleton | ||
@PreMatching | ||
public class LoadDetectFilter implements ContainerRequestFilter { | ||
|
||
@Context | ||
private javax.inject.Provider<HugeConfig> configProvider; | ||
@Context | ||
private javax.inject.Provider<WorkLoad> loadProvider; | ||
|
||
@Override | ||
public void filter(ContainerRequestContext context) throws IOException { | ||
HugeConfig config = this.configProvider.get(); | ||
int maxWorkerThreads = config.get(ServerOptions.MAX_WORKER_THREADS); | ||
WorkLoad load = this.loadProvider.get(); | ||
// There will be a thread doesn't work, dedicated to statistics | ||
if (load.incrementAndGet() >= maxWorkerThreads) { | ||
throw new ServiceUnavailableException( | ||
"The server is too busy to process the request, " + | ||
"please try again later"); | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
hugegraph-api/src/main/java/com/baidu/hugegraph/api/filter/LoadReleaseFilter.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,47 @@ | ||
/* | ||
* Copyright 2017 HugeGraph Authors | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.baidu.hugegraph.api.filter; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.inject.Singleton; | ||
import javax.ws.rs.container.ContainerRequestContext; | ||
import javax.ws.rs.container.ContainerResponseContext; | ||
import javax.ws.rs.container.ContainerResponseFilter; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.ext.Provider; | ||
|
||
import com.baidu.hugegraph.core.WorkLoad; | ||
|
||
@Provider | ||
@Singleton | ||
public class LoadReleaseFilter implements ContainerResponseFilter { | ||
|
||
@Context | ||
private javax.inject.Provider<WorkLoad> loadProvider; | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext, | ||
ContainerResponseContext responseContext) | ||
throws IOException { | ||
WorkLoad load = this.loadProvider.get(); | ||
load.decrementAndGet(); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
hugegraph-api/src/main/java/com/baidu/hugegraph/core/WorkLoad.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,51 @@ | ||
/* | ||
* Copyright 2017 HugeGraph Authors | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with this | ||
* work for additional information regarding copyright ownership. The ASF | ||
* licenses this file to You under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.baidu.hugegraph.core; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public final class WorkLoad { | ||
|
||
private final AtomicInteger load; | ||
|
||
public WorkLoad() { | ||
this(0); | ||
} | ||
|
||
public WorkLoad(int load) { | ||
this.load = new AtomicInteger(load); | ||
} | ||
|
||
public WorkLoad(AtomicInteger load) { | ||
this.load = load; | ||
} | ||
|
||
public AtomicInteger get() { | ||
return this.load; | ||
} | ||
|
||
public int incrementAndGet() { | ||
return this.load.incrementAndGet(); | ||
} | ||
|
||
public int decrementAndGet() { | ||
return this.load.decrementAndGet(); | ||
} | ||
} |
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