-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing the SerializationDateTimeConversionMode `AlwaysEpochMillisecds…
…WithSystemDefaultTimezone` (#30266) * Fixing the SerializationDateTimeConversionMode `AlwaysEpochMillisecondsWithSystemDefaultTimezone` The `CosmosRowConverter` had a bug assuming that ZoneOffsets between two time zones would be constant - in fact in the 19xx years political rules defining summer/winter time have changed multiple times. To get the correct ZoneOffset the right "local" Instant (vs. Instant.now) needs to be used. * Adding changelog * Adding UDF to vonvert back ChangeFeedOffset to Map[Int, Long] to make it possible to save in hdfs as Spark2 continuations * Preparing azure-cosmos-spark 4.12.2 release * Updating test for time conversion
- Loading branch information
1 parent
dac0c7a
commit caddd5a
Showing
13 changed files
with
224 additions
and
41 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
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
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
120 changes: 120 additions & 0 deletions
120
...main/scala/com/azure/cosmos/spark/udf/CreateSpark2ContinuationsFromChangeFeedOffset.scala
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,120 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
package com.azure.cosmos.spark.udf | ||
|
||
import com.azure.cosmos.{CosmosAsyncClient, SparkBridgeInternal} | ||
import com.azure.cosmos.implementation.SparkBridgeImplementationInternal | ||
import com.azure.cosmos.implementation.SparkBridgeImplementationInternal.rangeToNormalizedRange | ||
import com.azure.cosmos.implementation.changefeed.common.ChangeFeedState | ||
import com.azure.cosmos.implementation.query.CompositeContinuationToken | ||
import com.azure.cosmos.spark.{ChangeFeedOffset, CosmosClientCache, CosmosClientCacheItem, CosmosClientConfiguration, CosmosConfig, CosmosContainerConfig, Loan} | ||
import org.apache.spark.sql.api.java.UDF2 | ||
|
||
import scala.collection.mutable | ||
|
||
@SerialVersionUID(1L) | ||
class CreateSpark2ContinuationsFromChangeFeedOffset extends UDF2[Map[String, String], String, Map[Int, Long]] { | ||
override def call | ||
( | ||
userProvidedConfig: Map[String, String], | ||
changeFeedOffset: String | ||
): Map[Int, Long] = { | ||
|
||
val effectiveUserConfig = CosmosConfig.getEffectiveConfig(None, None, userProvidedConfig) | ||
val cosmosClientConfig = CosmosClientConfiguration( | ||
effectiveUserConfig, | ||
useEventualConsistency = false) | ||
|
||
val cosmosContainerConfig: CosmosContainerConfig = | ||
CosmosContainerConfig.parseCosmosContainerConfig(effectiveUserConfig, None, None) | ||
|
||
Loan( | ||
List[Option[CosmosClientCacheItem]]( | ||
Some(CosmosClientCache( | ||
cosmosClientConfig, | ||
None, | ||
s"UDF CreateSpark2ContinuationsFromChangeFeedOffset" | ||
)) | ||
)) | ||
.to(cosmosClientCacheItems => { | ||
createSpark2ContinuationsFromChangeFeedOffset( | ||
cosmosClientCacheItems.head.get.client, | ||
cosmosContainerConfig.database, | ||
cosmosContainerConfig.container, | ||
changeFeedOffset | ||
) | ||
}) | ||
} | ||
|
||
private[this] def createSpark2ContinuationsFromChangeFeedOffset | ||
( | ||
client: CosmosAsyncClient, | ||
databaseName: String, | ||
containerName: String, | ||
offsetJson: String | ||
): Map[Int, Long] = { | ||
|
||
val effectiveOffsetJson = if (offsetJson.indexOf("\n") == 2 && offsetJson.size > 2) { | ||
offsetJson.substring(3) | ||
} else { | ||
offsetJson | ||
} | ||
val offset: ChangeFeedOffset = ChangeFeedOffset.fromJson(effectiveOffsetJson) | ||
|
||
val container = client | ||
.getDatabase(databaseName) | ||
.getContainer(containerName) | ||
|
||
val expectedContainerResourceId = container.read().block().getProperties.getResourceId | ||
|
||
val pkRanges = SparkBridgeInternal | ||
.getPartitionKeyRanges(container) | ||
|
||
val lsnsByPkRangeId = mutable.Map[Int, Long]() | ||
|
||
pkRanges | ||
.foreach(pkRange => { | ||
val normalizedRange = rangeToNormalizedRange(pkRange.toRange) | ||
|
||
val effectiveChangeFeedState = ChangeFeedState | ||
.fromString( | ||
SparkBridgeImplementationInternal | ||
.extractChangeFeedStateForRange(offset.changeFeedState, normalizedRange) | ||
) | ||
|
||
val containerResourceId = effectiveChangeFeedState.getContainerRid | ||
|
||
if (!expectedContainerResourceId.equalsIgnoreCase(containerResourceId)) { | ||
throw new IllegalArgumentException( | ||
s"The provided change feed offset is for a different container (either completely different container " + | ||
s"or container with same name but after being deleted and recreated). Name:$containerName " + | ||
s"Expected ResourceId: $expectedContainerResourceId, " + | ||
s"Actual ResourceId: $containerResourceId" | ||
) | ||
} | ||
|
||
var minLsn: Option[CompositeContinuationToken] = None | ||
|
||
effectiveChangeFeedState | ||
.extractContinuationTokens() | ||
.forEach(token => { | ||
|
||
if (minLsn.isEmpty) { | ||
minLsn = Some(token) | ||
} else if (SparkBridgeImplementationInternal.toLsn(token.getToken) < | ||
SparkBridgeImplementationInternal.toLsn(minLsn.get.getToken)) { | ||
minLsn = Some(token) | ||
} | ||
}) | ||
|
||
if (minLsn.isDefined) { | ||
lsnsByPkRangeId.put( | ||
pkRange.getId.toInt, | ||
SparkBridgeImplementationInternal.toLsn(minLsn.get.getToken)) | ||
} | ||
}) | ||
|
||
lsnsByPkRangeId.toMap | ||
} | ||
} | ||
|
Oops, something went wrong.