-
Notifications
You must be signed in to change notification settings - Fork 15
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
Issue 173 struct evolution #174
Merged
Merged
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
25560f7
Initial commit with flakey test
0000ac5
Updates to test
42dc731
Updates to test
2d0d100
Adding column in between two columns
56ab02c
Some changes to use Parquet
85b74ba
Test now failing successfully
a548c95
Creating AlterTableService and operations
0f6ff36
Fixing integration test
c304abc
Fixing integration test
8638af1
Fixing unit tests and copyright
8d5eded
Some fixes
12aa815
Adding DropTableService which removes custom table parameters before …
e1b2eac
Adding changelog and other bits
4ce2b90
Added catch for non existent table
7ee9d23
Fixing some bits, renaming, adding external key to table when dropping
94ea2ee
Undoing unnecessary changes
890ea8d
Fixes
2b93368
Fixing pom
2b2664f
Fixing pom and test names
e8cd3a8
Minor change
4e40d29
Merge branch 'master' into issue-173-struct-evolution
971e669
Fixing tests
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
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
81 changes: 81 additions & 0 deletions
81
...in-core/src/main/java/com/hotels/bdp/circustrain/core/replica/hive/AlterTableService.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,81 @@ | ||
/** | ||
* Copyright (C) 2016-2020 Expedia, Inc. | ||
* | ||
* Licensed 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.hotels.bdp.circustrain.core.replica.hive; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.hadoop.hive.metastore.api.FieldSchema; | ||
import org.apache.hadoop.hive.metastore.api.Table; | ||
import org.apache.thrift.TException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.hotels.bdp.circustrain.core.replica.Replica; | ||
import com.hotels.hcommon.hive.metastore.client.api.CloseableMetaStoreClient; | ||
|
||
public class AlterTableService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(Replica.class); | ||
|
||
private CopyPartitionsOperation copyPartitionsOperation; | ||
private RenameTableOperation renameTableOperation; | ||
|
||
public AlterTableService( | ||
CopyPartitionsOperation copyPartitionsOperation, | ||
RenameTableOperation renameTableOperation) { | ||
this.copyPartitionsOperation = copyPartitionsOperation; | ||
this.renameTableOperation = renameTableOperation; | ||
} | ||
|
||
public void alterTable(CloseableMetaStoreClient client, Table oldTable, Table newTable) throws TException { | ||
List<FieldSchema> oldColumns = oldTable.getSd().getCols(); | ||
List<FieldSchema> newColumns = newTable.getSd().getCols(); | ||
if (hasAnyChangedColumns(oldColumns, newColumns)) { | ||
LOG | ||
.info("Found columns that have changed type, attempting to recreate target table with the new columns." | ||
+ "Old columns: {}, new columns: {}", oldColumns, newColumns); | ||
Table tempTable = new Table(newTable); | ||
String tempName = newTable.getTableName() + "_temp"; | ||
tempTable.setTableName(tempName); | ||
try { | ||
client.createTable(tempTable); | ||
copyPartitionsOperation.execute(client, newTable, tempTable); | ||
renameTableOperation.execute(client, tempTable, newTable); | ||
} finally { | ||
client.dropTable(newTable.getDbName(), tempName, false, true); | ||
} | ||
} else { | ||
client.alter_table(newTable.getDbName(), newTable.getTableName(), newTable); | ||
} | ||
} | ||
|
||
private boolean hasAnyChangedColumns(List<FieldSchema> oldColumns, List<FieldSchema> newColumns) { | ||
Map<String, FieldSchema> oldColumnsMap = oldColumns.stream() | ||
.collect(Collectors.toMap(FieldSchema::getName, Function.identity())); | ||
for (FieldSchema newColumn : newColumns) { | ||
if (oldColumnsMap.containsKey(newColumn.getName())) { | ||
FieldSchema oldColumn = oldColumnsMap.get(newColumn.getName()); | ||
if (!oldColumn.getType().equals(newColumn.getType())) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...e/src/main/java/com/hotels/bdp/circustrain/core/replica/hive/CopyPartitionsOperation.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,76 @@ | ||
/** | ||
* Copyright (C) 2016-2020 Expedia, Inc. | ||
* | ||
* Licensed 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.hotels.bdp.circustrain.core.replica.hive; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.hadoop.hive.metastore.api.Partition; | ||
import org.apache.hadoop.hive.metastore.api.StorageDescriptor; | ||
import org.apache.hadoop.hive.metastore.api.Table; | ||
import org.apache.thrift.TException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import com.hotels.hcommon.hive.metastore.client.api.CloseableMetaStoreClient; | ||
import com.hotels.hcommon.hive.metastore.iterator.PartitionIterator; | ||
|
||
public class CopyPartitionsOperation { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CopyPartitionsOperation.class); | ||
private static final short DEFAULT_BATCH_SIZE = 1000; | ||
|
||
private short partitionBatchSize; | ||
|
||
public CopyPartitionsOperation() { | ||
this(DEFAULT_BATCH_SIZE); | ||
} | ||
|
||
@VisibleForTesting | ||
CopyPartitionsOperation(short partitionBatchSize) { | ||
this.partitionBatchSize = partitionBatchSize; | ||
} | ||
|
||
/** | ||
* Copies partitions from oldTable to newTable, partitions copied are modified to take the schema of newTable | ||
*/ | ||
public void execute(CloseableMetaStoreClient client, Table oldTable, Table newTable) throws TException { | ||
int count = 0; | ||
String databaseName = newTable.getDbName(); | ||
String tableName = newTable.getTableName(); | ||
PartitionIterator partitionIterator = new PartitionIterator(client, oldTable, partitionBatchSize); | ||
while (partitionIterator.hasNext()) { | ||
List<Partition> batch = new ArrayList<>(); | ||
for (int i = 0; i < partitionBatchSize && partitionIterator.hasNext(); i++) { | ||
Partition partition = partitionIterator.next(); | ||
count++; | ||
Partition copy = new Partition(partition); | ||
copy.setDbName(databaseName); | ||
copy.setTableName(tableName); | ||
StorageDescriptor sd = new StorageDescriptor(partition.getSd()); | ||
sd.setCols(newTable.getSd().getCols()); | ||
copy.setSd(sd); | ||
batch.add(copy); | ||
} | ||
LOG.info("Copying batch of size {} to {}.{}", batch.size(), databaseName, tableName); | ||
client.add_partitions(batch); | ||
} | ||
LOG.info("Copied {} partitions to {}.{}", count, databaseName, tableName); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
...core/src/main/java/com/hotels/bdp/circustrain/core/replica/hive/RenameTableOperation.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,54 @@ | ||
/** | ||
* Copyright (C) 2016-2020 Expedia, Inc. | ||
* | ||
* Licensed 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.hotels.bdp.circustrain.core.replica.hive; | ||
|
||
import org.apache.hadoop.hive.metastore.api.Table; | ||
import org.apache.thrift.TException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.hotels.hcommon.hive.metastore.client.api.CloseableMetaStoreClient; | ||
|
||
public class RenameTableOperation { | ||
|
||
private final static Logger LOG = LoggerFactory.getLogger(RenameTableOperation.class); | ||
|
||
/** | ||
* <p> | ||
* NOTE: assumes both `from` and `to` exist | ||
* </p> | ||
* Renames tables 'from' table into 'to' table, at the end of the operation 'from' will be gone and 'to' will be | ||
* renamed. | ||
*/ | ||
public void execute(CloseableMetaStoreClient client, Table from, Table to) throws TException { | ||
LOG | ||
.info("Renaming table {}.{} to {}.{}", from.getDbName(), from.getTableName(), to.getDbName(), | ||
to.getTableName()); | ||
from = client.getTable(from.getDbName(), from.getTableName()); | ||
patduin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
to = client.getTable(to.getDbName(), to.getTableName()); | ||
String fromTableName = from.getTableName(); | ||
String toTableName = to.getTableName(); | ||
String toTableNameTemp = toTableName + "_original"; | ||
try { | ||
from.setTableName(toTableName); | ||
to.setTableName(toTableNameTemp); | ||
client.alter_table(to.getDbName(), toTableName, to); | ||
client.alter_table(from.getDbName(), fromTableName, from); | ||
} finally { | ||
client.dropTable(to.getDbName(), toTableNameTemp, false, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to double check beekeeper props and we don't accidentally delete folders when we drop this table. |
||
} | ||
} | ||
} |
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.
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.
Does this copy all the table properties?
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.
It does indeed. We can either (1) scrub all the table params here, or (2) I've created a
DropTableService
which removes custom table params before calling the drop table command. If thats overly complicated I can remove the new service and just do (1) instead.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.
OK so if we removed the params here then we don't have to worry about the potential of Beekeeper picking up the drop of the temp table etc.? I think I prefer that approach unless some of the params are useful to have on the temp table? I don't think any of the intermediate operations use them in any way so probably not? And to be clear, the original table params will be kept on the final end result table?
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.
Okay cool, I'm happy to do this, I'll remove the DropTableService, and just remove the params as part of this service.
The intermediate ops dont use the params, but i think beekeeper may pick up on the alter partition events (though will ignore them as the location doesnt change). And yes, the original params will be on the final table - i'll add some tests for this.
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.
Tried this out, and actually it doesn't seem possible. We can't do an alter table on the table as it still has the old schema, so it fails with the same error. So the DropTableService looks better as we do the alter/drop once everything has been updated.
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.
OK, let's go with that for now and keep an eye on it when this is in use.