|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.common.proc; |
| 19 | + |
| 20 | +import org.apache.doris.common.AnalysisException; |
| 21 | +import org.apache.doris.common.Config; |
| 22 | +import org.apache.doris.common.io.Text; |
| 23 | +import org.apache.doris.common.jmockit.Deencapsulation; |
| 24 | +import org.apache.doris.journal.JournalEntity; |
| 25 | +import org.apache.doris.journal.bdbje.BDBDebugger; |
| 26 | +import org.apache.doris.persist.OperationType; |
| 27 | + |
| 28 | +import com.google.common.collect.Lists; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.Assertions; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.mockito.Mockito; |
| 34 | + |
| 35 | +import java.io.ByteArrayInputStream; |
| 36 | +import java.io.ByteArrayOutputStream; |
| 37 | +import java.io.DataInputStream; |
| 38 | +import java.io.DataOutputStream; |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +/** |
| 43 | + * SHOW PROC '/bdbje/dbName/journalId', the BDBJE debug-mode view of a single journal record. |
| 44 | + */ |
| 45 | +public class BdbjeJournalDataProcNodeTest { |
| 46 | + private static final String DB_NAME = "1"; |
| 47 | + |
| 48 | + private boolean debugMode; |
| 49 | + private BDBDebugger.BDBDebugEnv debugEnv; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + public void setUp() { |
| 53 | + debugMode = Config.enable_bdbje_debug_mode; |
| 54 | + debugEnv = BDBDebugger.get().getEnv(); |
| 55 | + } |
| 56 | + |
| 57 | + @AfterEach |
| 58 | + public void tearDown() { |
| 59 | + Config.enable_bdbje_debug_mode = debugMode; |
| 60 | + Deencapsulation.setField(BDBDebugger.get(), "debugEnv", debugEnv); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void recordsOfRetiredOperationTypesArePrintable() throws Exception { |
| 65 | + // The readers of retired operation types consume the payload without keeping it. Op 500 is the Lance |
| 66 | + // index job of unreleased branch-4.x builds, written as Text-framed JSON; the PL/SQL operation types were |
| 67 | + // retired the same way. |
| 68 | + BDBDebugger.JournalEntityWrapper lanceIndexJob = record(7L, OperationType.OP_LANCE_INDEX_JOB_UPSERT, |
| 69 | + "{\"jid\":7,\"rev\":2,\"cid\":10101,\"dbn\":\"db1\",\"tbn\":\"tbl1\",\"iid\":\"idx_v\"," |
| 70 | + + "\"it\":\"IVF_PQ\",\"cn\":[\"v\"],\"rs\":\"RUNNING\"}"); |
| 71 | + BDBDebugger.JournalEntityWrapper plsqlPackage = record(8L, OperationType.OP_DROP_PLSQL_PACKAGE, |
| 72 | + "{\"name\":\"pkg1\",\"catalogId\":0,\"dbName\":\"db1\"}"); |
| 73 | + Assertions.assertNull(lanceIndexJob.entity.getData()); |
| 74 | + Assertions.assertNull(plsqlPackage.entity.getData()); |
| 75 | + installDebugEnv(lanceIndexJob, plsqlPackage); |
| 76 | + |
| 77 | + // MUTATION: with getData().toString() both rows fail with a NullPointerException. |
| 78 | + Assertions.assertEquals(Lists.newArrayList("7", "OP_LANCE_INDEX_JOB_UPSERT", |
| 79 | + lanceIndexJob.size.toString(), "null"), fetchRow(7L)); |
| 80 | + Assertions.assertEquals(Lists.newArrayList("8", "OP_DROP_PLSQL_PACKAGE", |
| 81 | + plsqlPackage.size.toString(), "null"), fetchRow(8L)); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void liveRecordStillShowsItsPayload() throws Exception { |
| 86 | + BDBDebugger.JournalEntityWrapper nextId = record(9L, OperationType.OP_SAVE_NEXTID, "12345"); |
| 87 | + installDebugEnv(nextId); |
| 88 | + |
| 89 | + Assertions.assertEquals(Lists.newArrayList("9", "OP_SAVE_NEXTID", nextId.size.toString(), "12345"), |
| 90 | + fetchRow(9L)); |
| 91 | + } |
| 92 | + |
| 93 | + /** A journal record as BDBDebugEnv#getJournalEntity reads it back from BDBJE. */ |
| 94 | + private static BDBDebugger.JournalEntityWrapper record(long journalId, short opCode, String payload) |
| 95 | + throws IOException { |
| 96 | + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 97 | + try (DataOutputStream out = new DataOutputStream(bytes)) { |
| 98 | + out.writeShort(opCode); |
| 99 | + // The int-length framing that both Text#write and Text.writeString produce. |
| 100 | + Text.writeString(out, payload); |
| 101 | + } |
| 102 | + BDBDebugger.JournalEntityWrapper wrapper = new BDBDebugger.JournalEntityWrapper(journalId); |
| 103 | + wrapper.size = bytes.size(); |
| 104 | + JournalEntity entity = new JournalEntity(); |
| 105 | + try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { |
| 106 | + entity.readFields(in); |
| 107 | + Assertions.assertEquals(-1, in.read(), "the record must be consumed exactly"); |
| 108 | + } |
| 109 | + wrapper.entity = entity; |
| 110 | + return wrapper; |
| 111 | + } |
| 112 | + |
| 113 | + private static void installDebugEnv(BDBDebugger.JournalEntityWrapper... records) { |
| 114 | + BDBDebugger.BDBDebugEnv env = Mockito.mock(BDBDebugger.BDBDebugEnv.class); |
| 115 | + for (BDBDebugger.JournalEntityWrapper record : records) { |
| 116 | + Mockito.when(env.getJournalEntity(DB_NAME, record.journalId)).thenReturn(record); |
| 117 | + } |
| 118 | + Deencapsulation.setField(BDBDebugger.get(), "debugEnv", env); |
| 119 | + Config.enable_bdbje_debug_mode = true; |
| 120 | + } |
| 121 | + |
| 122 | + private static List<String> fetchRow(long journalId) throws AnalysisException { |
| 123 | + ProcResult result = new BdbjeJournalDataProcNode(DB_NAME, journalId).fetchResult(); |
| 124 | + Assertions.assertEquals(BdbjeJournalDataProcNode.TITLE_NAMES, result.getColumnNames()); |
| 125 | + Assertions.assertEquals(1, result.getRows().size()); |
| 126 | + return result.getRows().get(0); |
| 127 | + } |
| 128 | +} |
0 commit comments