forked from apache/inlong
-
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.
[INLONG-9120][Agent] Add offset db to store the offset data (apache#9121
) * [INLONG-9120][Agent] Add offset db to store the offset data * [INLONG-9120][Agent] Add offset db to store the offset data
- Loading branch information
1 parent
636822b
commit 3ee9921
Showing
3 changed files
with
96 additions
and
3 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
83 changes: 83 additions & 0 deletions
83
inlong-agent/agent-common/src/main/java/org/apache/inlong/agent/db/OffsetDb.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,83 @@ | ||
/* | ||
* 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 org.apache.inlong.agent.db; | ||
|
||
import org.apache.inlong.agent.conf.AgentConfiguration; | ||
import org.apache.inlong.agent.conf.OffsetProfile; | ||
import org.apache.inlong.agent.constant.AgentConstants; | ||
import org.apache.inlong.agent.constant.CommonConstants; | ||
import org.apache.inlong.agent.constant.TaskConstants; | ||
import org.apache.inlong.agent.utils.AgentUtils; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* db interface for task profile. | ||
*/ | ||
public class OffsetDb { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(OffsetDb.class); | ||
private final Db db; | ||
private final AgentConfiguration agentConf; | ||
|
||
public OffsetDb() { | ||
agentConf = AgentConfiguration.getAgentConf(); | ||
db = initDb(agentConf.get(AgentConstants.AGENT_ROCKS_DB_PATH, AgentConstants.AGENT_LOCAL_DB_PATH_OFFSET)); | ||
} | ||
|
||
/** | ||
* init db by class name | ||
* | ||
* @return db | ||
*/ | ||
private Db initDb(String childPath) { | ||
try { | ||
return new RocksDbImp(childPath); | ||
} catch (Exception ex) { | ||
throw new UnsupportedClassVersionError(ex.getMessage()); | ||
} | ||
} | ||
|
||
public OffsetProfile getOffset(String taskId, String instanceId) { | ||
KeyValueEntity result = db.get(getKey(taskId, instanceId)); | ||
if (result == null) { | ||
return null; | ||
} | ||
return result.getAsOffsetProfile(); | ||
} | ||
|
||
public void deleteOffset(String taskId, String instanceId) { | ||
db.remove(getKey(taskId, instanceId)); | ||
} | ||
|
||
public void setOffset(OffsetProfile offsetProfile) { | ||
offsetProfile.setLastUpdateTime(AgentUtils.getCurrentTime()); | ||
if (offsetProfile.allRequiredKeyExist()) { | ||
String keyName = getKey(offsetProfile.getTaskId(), | ||
offsetProfile.getInstanceId()); | ||
KeyValueEntity entity = new KeyValueEntity(keyName, | ||
offsetProfile.toJsonStr(), offsetProfile.get(TaskConstants.INSTANCE_ID)); | ||
db.put(entity); | ||
} | ||
} | ||
|
||
private String getKey(String taskId, String instanceId) { | ||
return CommonConstants.OFFSET_ID_PREFIX + taskId + "_" + instanceId; | ||
} | ||
} |