|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright 2018-2020 The Feast Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package feast.core.job; |
| 18 | + |
| 19 | +import com.google.common.collect.Lists; |
| 20 | +import feast.core.dao.JobRepository; |
| 21 | +import feast.core.model.Job; |
| 22 | +import feast.core.model.JobStatus; |
| 23 | +import feast.core.model.Source; |
| 24 | +import feast.core.model.Store; |
| 25 | +import java.time.Instant; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | +import org.apache.commons.lang3.tuple.Pair; |
| 33 | + |
| 34 | +/** |
| 35 | + * In this strategy one job per Source-Store pair is created. |
| 36 | + * |
| 37 | + * <p>JobId is generated accordingly from Source (type+config) and StoreName. |
| 38 | + */ |
| 39 | +public class JobPerStoreStrategy implements JobGroupingStrategy { |
| 40 | + private final JobRepository jobRepository; |
| 41 | + |
| 42 | + public JobPerStoreStrategy(JobRepository jobRepository) { |
| 43 | + this.jobRepository = jobRepository; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Job getOrCreateJob(Source source, Set<Store> stores) { |
| 48 | + ArrayList<Store> storesList = Lists.newArrayList(stores); |
| 49 | + if (storesList.size() != 1) { |
| 50 | + throw new RuntimeException("Only one store is acceptable in JobPerStore Strategy"); |
| 51 | + } |
| 52 | + Store store = storesList.get(0); |
| 53 | + |
| 54 | + return jobRepository |
| 55 | + .findFirstBySourceTypeAndSourceConfigAndStoreNameAndStatusNotInOrderByLastUpdatedDesc( |
| 56 | + source.getType(), source.getConfig(), store.getName(), JobStatus.getTerminalStates()) |
| 57 | + .orElseGet( |
| 58 | + () -> |
| 59 | + Job.builder() |
| 60 | + .setSource(source) |
| 61 | + .setStoreName(store.getName()) |
| 62 | + .setStores(stores) |
| 63 | + .setFeatureSetJobStatuses(new HashSet<>()) |
| 64 | + .build()); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String createJobId(Job job) { |
| 69 | + String dateSuffix = String.valueOf(Instant.now().toEpochMilli()); |
| 70 | + String jobId = |
| 71 | + String.format( |
| 72 | + "%s-%d-to-%s-%s", |
| 73 | + job.getSource().getTypeString(), |
| 74 | + Objects.hashCode(job.getSource().getConfig()), |
| 75 | + job.getStoreName(), |
| 76 | + dateSuffix); |
| 77 | + return jobId.replaceAll("_store", "-").toLowerCase(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public Iterable<Pair<Source, Set<Store>>> collectSingleJobInput( |
| 82 | + Stream<Pair<Source, Store>> stream) { |
| 83 | + return stream.map(p -> Pair.of(p.getLeft(), Set.of(p.getRight()))).collect(Collectors.toList()); |
| 84 | + } |
| 85 | +} |
0 commit comments