-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BIGTOP-4152: Add task log gRPC service to read agent logs (#14)
- Loading branch information
Showing
19 changed files
with
272 additions
and
156 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
bigtop-manager-agent/src/main/java/org/apache/bigtop/manager/agent/cache/Caches.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,26 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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 org.apache.bigtop.manager.agent.cache; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Caches { | ||
public static final List<Long> RUNNING_TASKS = new ArrayList<>(); | ||
} |
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
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
103 changes: 103 additions & 0 deletions
103
...r-agent/src/main/java/org/apache/bigtop/manager/agent/service/TaskLogServiceGrpcImpl.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,103 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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 org.apache.bigtop.manager.agent.service; | ||
|
||
import org.apache.bigtop.manager.agent.cache.Caches; | ||
import org.apache.bigtop.manager.grpc.generated.TaskLogReply; | ||
import org.apache.bigtop.manager.grpc.generated.TaskLogRequest; | ||
import org.apache.bigtop.manager.grpc.generated.TaskLogServiceGrpc; | ||
|
||
import org.apache.commons.lang3.SystemUtils; | ||
|
||
import io.grpc.Status; | ||
import io.grpc.stub.StreamObserver; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.devh.boot.grpc.server.service.GrpcService; | ||
|
||
import java.io.File; | ||
import java.io.RandomAccessFile; | ||
|
||
@Slf4j | ||
@GrpcService | ||
public class TaskLogServiceGrpcImpl extends TaskLogServiceGrpc.TaskLogServiceImplBase { | ||
|
||
@Override | ||
public void getLog(TaskLogRequest request, StreamObserver<TaskLogReply> responseObserver) { | ||
String path = getLogFilePath(request.getTaskId()); | ||
try (RandomAccessFile file = new RandomAccessFile(path, "r")) { | ||
// Read from beginning | ||
long fileLength = file.length(); | ||
while (file.getFilePointer() < fileLength) { | ||
String line = file.readLine(); | ||
if (line != null) { | ||
responseObserver.onNext( | ||
TaskLogReply.newBuilder().setText(line).build()); | ||
} | ||
} | ||
|
||
// Waiting for new logs | ||
boolean isTaskRunning = true; | ||
while (isTaskRunning) { | ||
isTaskRunning = Caches.RUNNING_TASKS.contains(request.getTaskId()); | ||
readNewLogs(file, responseObserver); | ||
Thread.sleep(1000); | ||
} | ||
} catch (Exception e) { | ||
String errMsg = "Error when reading task log: " + e.getMessage() + ", please fix it"; | ||
responseObserver.onNext(TaskLogReply.newBuilder().setText(errMsg).build()); | ||
|
||
log.error("Error reading task log", e); | ||
Status status = Status.UNKNOWN.withDescription(e.getMessage()); | ||
responseObserver.onError(status.asRuntimeException()); | ||
} | ||
} | ||
|
||
private void readNewLogs(RandomAccessFile file, StreamObserver<TaskLogReply> responseObserver) throws Exception { | ||
long position = file.getFilePointer(); | ||
if (position < file.length()) { | ||
// Read new logs | ||
file.seek(position); | ||
if (file.readByte() != '\n') { | ||
file.seek(position); | ||
} | ||
|
||
String line = file.readLine(); | ||
while (line != null) { | ||
responseObserver.onNext(TaskLogReply.newBuilder().setText(line).build()); | ||
line = file.readLine(); | ||
} | ||
} | ||
} | ||
|
||
private String getLogFilePath(Long taskId) { | ||
String baseDir; | ||
if (SystemUtils.IS_OS_WINDOWS) { | ||
baseDir = SystemUtils.getUserDir().getPath(); | ||
} else { | ||
File file = new File(this.getClass() | ||
.getProtectionDomain() | ||
.getCodeSource() | ||
.getLocation() | ||
.getPath()); | ||
baseDir = file.getParentFile().getParentFile().getPath(); | ||
} | ||
|
||
return baseDir + File.separator + "tasklogs" + File.separator + "task-" + taskId + ".log"; | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
bigtop-manager-grpc/src/main/resources/proto/task_log.proto
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,35 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
syntax = "proto3"; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "org.apache.bigtop.manager.grpc.generated"; | ||
option java_outer_classname = "TaskLogProto"; | ||
|
||
service TaskLogService { | ||
rpc GetLog (TaskLogRequest) returns (stream TaskLogReply) {} | ||
} | ||
|
||
message TaskLogRequest { | ||
int64 task_id = 1; | ||
} | ||
|
||
message TaskLogReply { | ||
string text = 1; | ||
} |
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
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
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
Oops, something went wrong.