-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[flink] Extract RefreshBlacklist from FileStoreLookupFunction
- Loading branch information
1 parent
3ff13dc
commit c17cdd5
Showing
2 changed files
with
117 additions
and
69 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
112 changes: 112 additions & 0 deletions
112
...nk/paimon-flink-common/src/main/java/org/apache/paimon/flink/lookup/RefreshBlacklist.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,112 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 org.apache.paimon.flink.lookup; | ||
|
||
import org.apache.paimon.utils.DateTimeUtils; | ||
import org.apache.paimon.utils.Pair; | ||
import org.apache.paimon.utils.StringUtils; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.format.DateTimeParseException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.TimeZone; | ||
|
||
/** Refresh black list for {@link FileStoreLookupFunction}. */ | ||
public class RefreshBlacklist { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(RefreshBlacklist.class); | ||
|
||
private final List<Pair<Long, Long>> timePeriodsBlacklist; | ||
|
||
private long nextBlacklistCheckTime; | ||
|
||
public RefreshBlacklist(String blacklist) { | ||
this.timePeriodsBlacklist = parseTimePeriodsBlacklist(blacklist); | ||
this.nextBlacklistCheckTime = -1; | ||
} | ||
|
||
private List<Pair<Long, Long>> parseTimePeriodsBlacklist(String blacklist) { | ||
if (StringUtils.isNullOrWhitespaceOnly(blacklist)) { | ||
return Collections.emptyList(); | ||
} | ||
String[] timePeriods = blacklist.split(","); | ||
List<Pair<Long, Long>> result = new ArrayList<>(); | ||
for (String period : timePeriods) { | ||
String[] times = period.split("->"); | ||
if (times.length != 2) { | ||
throw new IllegalArgumentException( | ||
String.format("Incorrect time periods format: [%s].", blacklist)); | ||
} | ||
|
||
long left = parseToMillis(times[0]); | ||
long right = parseToMillis(times[1]); | ||
if (left > right) { | ||
throw new IllegalArgumentException( | ||
String.format("Incorrect time period: [%s->%s].", times[0], times[1])); | ||
} | ||
result.add(Pair.of(left, right)); | ||
} | ||
return result; | ||
} | ||
|
||
private long parseToMillis(String dateTime) { | ||
try { | ||
return DateTimeUtils.parseTimestampData(dateTime + ":00", 3, TimeZone.getDefault()) | ||
.getMillisecond(); | ||
} catch (DateTimeParseException e) { | ||
throw new IllegalArgumentException( | ||
String.format("Date time format error: [%s].", dateTime), e); | ||
} | ||
} | ||
|
||
public boolean canRefresh() { | ||
long currentTimeMillis = System.currentTimeMillis(); | ||
if (currentTimeMillis < nextBlacklistCheckTime) { | ||
return false; | ||
} | ||
|
||
Pair<Long, Long> selectedPeriod = null; | ||
for (Pair<Long, Long> period : timePeriodsBlacklist) { | ||
if (period.getLeft() <= currentTimeMillis && currentTimeMillis <= period.getRight()) { | ||
selectedPeriod = period; | ||
break; | ||
} | ||
} | ||
|
||
if (selectedPeriod != null) { | ||
LOG.info( | ||
"Current time {} is in black list {}-{}, so try to refresh cache next time.", | ||
currentTimeMillis, | ||
selectedPeriod.getLeft(), | ||
selectedPeriod.getRight()); | ||
nextBlacklistCheckTime = selectedPeriod.getRight() + 1; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public long nextBlacklistCheckTime() { | ||
return nextBlacklistCheckTime; | ||
} | ||
} |