-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Replay command topic to local file to backup KSQL Metastore #5831
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
102 changes: 102 additions & 0 deletions
102
ksqldb-rest-app/src/main/java/io/confluent/ksql/rest/server/BackupReplayFile.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,102 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.rest.server; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.confluent.ksql.execution.json.PlanJsonMapper; | ||
import io.confluent.ksql.rest.entity.CommandId; | ||
import io.confluent.ksql.rest.server.computation.Command; | ||
import io.confluent.ksql.util.KsqlException; | ||
import io.confluent.ksql.util.Pair; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.Closeable; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStreamWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* A file that is used by the backup service to replay command_topic commands. | ||
*/ | ||
public class BackupReplayFile implements Closeable { | ||
private static final ObjectMapper MAPPER = PlanJsonMapper.INSTANCE.get(); | ||
private static final String KEY_VALUE_SEPARATOR = ":"; | ||
|
||
private final File file; | ||
private final BufferedWriter writer; | ||
|
||
public BackupReplayFile(final File file) { | ||
this.file = Objects.requireNonNull(file, "file"); | ||
this.writer = createWriter(file); | ||
} | ||
|
||
private static BufferedWriter createWriter(final File file) { | ||
try { | ||
return new BufferedWriter(new OutputStreamWriter( | ||
new FileOutputStream(file, true), | ||
StandardCharsets.UTF_8) | ||
); | ||
} catch (final FileNotFoundException e) { | ||
throw new KsqlException( | ||
String.format("Failed to create replay file: %s", file.getAbsolutePath()), e); | ||
} | ||
} | ||
|
||
public String getPath() { | ||
return file.getAbsolutePath(); | ||
} | ||
|
||
public void write(final CommandId commandId, final Command command) throws IOException { | ||
writer.write(MAPPER.writeValueAsString(commandId)); | ||
writer.write(KEY_VALUE_SEPARATOR); | ||
writer.write(MAPPER.writeValueAsString(command)); | ||
writer.write("\n"); | ||
writer.flush(); | ||
} | ||
|
||
public void write(final List<Pair<CommandId, Command>> records) throws IOException { | ||
for (final Pair<CommandId, Command> record : records) { | ||
write(record.left, record.right); | ||
} | ||
} | ||
|
||
public List<Pair<CommandId, Command>> readRecords() throws IOException { | ||
final List<Pair<CommandId, Command>> commands = new ArrayList<>(); | ||
for (final String line : Files.readAllLines(file.toPath(), StandardCharsets.UTF_8)) { | ||
final String commandId = line.substring(0, line.indexOf(KEY_VALUE_SEPARATOR)); | ||
final String command = line.substring(line.indexOf(KEY_VALUE_SEPARATOR) + 1); | ||
|
||
commands.add(new Pair<>( | ||
MAPPER.readValue(commandId.getBytes(StandardCharsets.UTF_8), CommandId.class), | ||
MAPPER.readValue(command.getBytes(StandardCharsets.UTF_8), Command.class) | ||
)); | ||
} | ||
|
||
return commands; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
writer.close(); | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
ksqldb-rest-app/src/main/java/io/confluent/ksql/rest/server/CommandTopicBackup.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 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.rest.server; | ||
|
||
import io.confluent.ksql.rest.entity.CommandId; | ||
import io.confluent.ksql.rest.server.computation.Command; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
||
public interface CommandTopicBackup { | ||
void initialize(); | ||
|
||
void writeRecord(ConsumerRecord<CommandId, Command> record); | ||
|
||
void close(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we test what happens if the command itself has a newline in it (or is it possible for the commandId to have the
KEY_VALUE_SEPARATOR
in it - I don't think so)? I know it will make it harder to replay the file, but it might make it safer if we have an encoding:commandIdSize (4 bytes) | commandSize (4 byte) | commandId | Command
another (perhaps better) option is that because we know that the CommandID and the Command are valid JSON, we can just read one valid JSON then the next (see https://stackoverflow.com/a/37395419/2258040) and we don't even have to worry about newlines (which we can add anyway to make it easier for humans to read)
I might be too paranoid, let me know if you don't think this is a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, I was looking on something like that. However, that format will not work with the plan of restoring the topic manually using this command:
If we have time, we could add a
ksql-restore
command that reads the file using your proposal (which I prefer). Perhaps a next release? I'm not sure if we can do it in a compatible way. Just changing the file name might be enough probably.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the proposed restore command work if the command has newlines? If not, then we need to fix that right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rodesai There's a test that verifies that in
BackupReplayFileTest.shouldWriteRecordWithNewLineCharacterInCommand
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline, according to the standard it's not possible for a json string field to have an embedded newline, so we should be good here. I still think a more explicit format (like what @agavra suggested) is safer (even if it means the backup file isn't immediately usable). Up to you.