Skip to content
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(batchUpdate): enhance batch update functionality #1483

Merged
merged 13 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@EqualsAndHashCode(of = {"id", "name", "application"})
public class Pipeline implements Timestamped {

public static final String TYPE_TEMPLATED = "templatedPipeline";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,63 +258,58 @@ class SqlStorageService(
}

override fun <T : Timestamped> storeObjects(objectType: ObjectType, allItems: Collection<T>) {
// using a lower `chunkSize` to avoid exceeding default packet size limits.
allItems.chunked(100).forEach { items ->
try {
withPool(poolName) {
jooq.transactional(sqlRetryProperties.transactions) { ctx ->
withPool(poolName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this flip, I think the entire batch fails now vs. only a chunk of it - intentional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess that's partly the point of this PR - just wondering if that's a good thing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can ignore this mostly just curious philosophically which is better :)

jooq.transactional(sqlRetryProperties.transactions) { ctx ->
// using a lower `chunkSize` to avoid exceeding default packet size limits.
allItems.chunked(100).forEach { items ->
try {
ctx.batch(
items.map { item ->
val insertPairs = definitionsByType[objectType]!!.getInsertPairs(
objectMapper, item.id.toLowerCase(), item
)
val updatePairs = definitionsByType[objectType]!!.getUpdatePairs(insertPairs)

ctx.insertInto(
table(definitionsByType[objectType]!!.tableName),
*insertPairs.keys.map { field(it) }.toTypedArray()
)
.values(insertPairs.values)
.onConflict(field("id", String::class.java))
.doUpdate()
.set(updatePairs.mapKeys { field(it.key) })
}
).execute()
} catch (e: SQLDialectNotSupportedException) {
for (item in items) {
storeSingleObject(objectType, item.id.toLowerCase(), item)
}
}

if (definitionsByType[objectType]!!.supportsHistory) {
try {
ctx.batch(
items.map { item ->
val insertPairs = definitionsByType[objectType]!!.getInsertPairs(
objectMapper, item.id.toLowerCase(), item
val historyPairs = definitionsByType[objectType]!!.getHistoryPairs(
objectMapper, clock, item.id.toLowerCase(), item
)
val updatePairs = definitionsByType[objectType]!!.getUpdatePairs(insertPairs)

ctx.insertInto(
table(definitionsByType[objectType]!!.tableName),
*insertPairs.keys.map { field(it) }.toTypedArray()
)
.values(insertPairs.values)
.onConflict(field("id", String::class.java))
.doUpdate()
.set(updatePairs.mapKeys { field(it.key) })
ctx
.insertInto(
table(definitionsByType[objectType]!!.historyTableName),
*historyPairs.keys.map { field(it) }.toTypedArray()
)
.values(historyPairs.values)
.onDuplicateKeyIgnore()
}
).execute()
} catch (e: SQLDialectNotSupportedException) {
for (item in items) {
storeSingleObject(objectType, item.id.toLowerCase(), item)
}
}

if (definitionsByType[objectType]!!.supportsHistory) {
try {
ctx.batch(
items.map { item ->
val historyPairs = definitionsByType[objectType]!!.getHistoryPairs(
objectMapper, clock, item.id.toLowerCase(), item
)

ctx
.insertInto(
table(definitionsByType[objectType]!!.historyTableName),
*historyPairs.keys.map { field(it) }.toTypedArray()
)
.values(historyPairs.values)
.onDuplicateKeyIgnore()
}
).execute()
} catch (e: SQLDialectNotSupportedException) {
for (item in items) {
storeSingleObjectHistory(objectType, item.id.toLowerCase(), item)
}
storeSingleObjectHistory(objectType, item.id.toLowerCase(), item)
}
}
}
}
} catch (e: Exception) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt this happens much - but any reason to lose this try/catch here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking TransientDaoException case where DB is failing or similar...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SHORT answer it'd be nice if these were caught & re-raised as Spinnaker exception objects

log.error("Unable to store objects (objectType: {}, objectKeys: {})", objectType, items.map { it.id })
throw e
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import dev.minutest.junit.JUnit5Minutests
import dev.minutest.rootContext
import java.time.Clock
import org.jooq.SQLDialect
import org.jooq.exception.DataAccessException
import org.jooq.impl.DSL
import org.jooq.impl.DSL.field
import org.jooq.impl.DSL.table
Expand Down Expand Up @@ -195,6 +196,37 @@ internal object SqlStorageServiceTests : JUnit5Minutests {
}
}

test("bulk create pipelines atomically") {
// verify that pipelines can be bulk created
val pipelines = (1..500).map { idx ->
Pipeline().apply {
id = "pipeline${idx}"
name = "pipeline${idx}"
lastModified = 100 + idx.toLong()
lastModifiedBy = "test"
setApplication("application")
}
}

// set lastModifiedBy of one of the pipelines to null in order to force an error
// and make sure no pipelines are added since additions are done in a single transaction
pipelines[250].lastModifiedBy = null
expectThrows<DataAccessException> {
sqlStorageService.storeObjects(ObjectType.PIPELINE,pipelines)
expectThat(
jooq.selectCount().from("pipelines").fetchOne(0, Int::class.java)
).isEqualTo(0)
}

// Reset lastModifiedBy to ensure successful bulk creation
pipelines[250].lastModifiedBy = "test"
sqlStorageService.storeObjects(ObjectType.PIPELINE,pipelines)

val storedPipelines = sqlStorageService.loadObjects<Pipeline>(ObjectType.PIPELINE, pipelines.map { it.id });
expectThat(storedPipelines.size).isEqualTo(500);
expectThat(storedPipelines.map { it.id }).isEqualTo(pipelines.map { it.id })
}

var lastModifiedMs : Long = 100
test("loadObjects basic behavior") {
val objectKeys = mutableSetOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.netflix.spinnaker.filters.AuthenticatedRequestFilter;
import com.netflix.spinnaker.front50.ItemDAOHealthIndicator;
import com.netflix.spinnaker.front50.api.validator.PipelineValidator;
import com.netflix.spinnaker.front50.config.controllers.PipelineControllerConfig;
import com.netflix.spinnaker.front50.model.application.ApplicationDAO;
import com.netflix.spinnaker.front50.model.application.ApplicationPermissionDAO;
import com.netflix.spinnaker.front50.model.delivery.DeliveryRepository;
Expand Down Expand Up @@ -58,7 +59,10 @@
@EnableFiatAutoConfig
@EnableScheduling
@Import({PluginsAutoConfiguration.class})
@EnableConfigurationProperties(StorageServiceConfigurationProperties.class)
@EnableConfigurationProperties({
StorageServiceConfigurationProperties.class,
PipelineControllerConfig.class
})
public class Front50WebConfig extends WebMvcConfigurerAdapter {

@Autowired private Registry registry;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024 Salesforce, 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.netflix.spinnaker.front50.config.controllers;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "controller.pipeline")
public class PipelineControllerConfig {

/** Holds the configurations to be used for save/update controller mappings */
private SavePipelineConfiguration save = new SavePipelineConfiguration();

@Data
public static class SavePipelineConfiguration {
/** This controls whether cache should be refreshes while checking for duplicate pipelines */
boolean refreshCacheOnDuplicatesCheck = true;
}
}
Loading