8000 Cache do not update per second one day · Issue #195 · cache2k/cache2k · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Cache do not update per second one day #195

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

Open
javalover123 opened this issue Oct 26, 2022 · 18 comments
Open

Cache do not update per second one day #195

javalover123 opened this issue Oct 26, 2022 · 18 comments

Comments

@javalover123
Copy link
javalover123 commented Oct 26, 2022

Cache do not update per second one day, and there no thread name starts with cache2k-loader-

static Cache<String, LocalDateTime> TIME_CACHE = new Cache2kBuilder<String, LocalDateTime>() {
    }.refreshAheadPolicy(new AutoRefreshPolicy<>())
            .expireAfterWrite(1, TimeUnit.SECONDS)
            .loader((key) -> LocalDateTime.now())
            .build();
@cruftex
Copy link
Member
cruftex commented Oct 28, 2022

Do you test with the latest SNAPSHOT code? Can you give a complete example of what you are trying with the cache and what you would expect, like a test case? Since you use the code that is currently in progress, it might be unfinished or a bug. Maybe its also a wrong assumption of the functionality.

@javalover123
Copy link
Author
javalover123 commented Oct 28, 2022

Yes, use 2.8-SNAPSHOT, we used 4 months, and it's right util this time.

I execute jstack, there saw only 2 cache2k-scheduler threads, then I restart the application and saw another cache2k-loader- thread.

Note: cache refresh after write 1 second.
TIME_CACHE.get(market)

@cruftex
Copy link
Member
cruftex commented Oct 28, 2022

Yes, use 2.8-SNAPSHOT, we used 4 months, and it's right util this time.

It would be better to use a released version. The SNAPSHOT might have varying quality.

Still, I am not sure what you are doing. Can you give me detailed information and steps how to reproduce your observations?

@javalover123
Copy link
Author
javalover123 commented Oct 28, 2022

Below is the simplest test code.

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.cache2k.expiry.RefreshAheadPolicy;
import org.junit.Test;

/**
 * Cache refresh failed one day. https://github.com/cache2k/cache2k/issues/195
 *
 * @author javalover123
 * @date 2022/10/28
 */
public class Cache2kRefreshTest {

    static Cache<String, LocalDateTime> TIME_CACHE = new Cache2kBuilder<String, LocalDateTime>() {
    }.refreshAheadPolicy(new AutoRefreshPolicy<>())
            .expireAfterWrite(1, TimeUnit.SECONDS)
            .timerLag(Duration.ofMillis(300))
            .loader((key) -> LocalDateTime.now())
            .build();

    public static class AutoRefreshPolicy<K, V, T> implements RefreshAheadPolicy<K, V, T> {

        @Override
        public long refreshAheadTime(Context<T> ctx) {
            return ctx.getExpiryTime();
        }

        @Override
        public int requiredHits(Context<T> ctx) {
            return 0;
        }

    }

    @Test
    public void refresh() throws InterruptedException {
        for (int i = 1; i <= 100000; i++) {
            final LocalDateTime now = LocalDateTime.now();
            final LocalDateTime cacheTime = TIME_CACHE.get("");
            if (cacheTime.plusSeconds(3).isBefore(now)) {
                System.err.println(now + ",refresh error," + cacheTime);
            }
            Thread.sleep(100);
        }
        System.out.println(LocalDateTime.now() + ",refresh done");
    }

}

@cruftex
Copy link
Member
cruftex commented Oct 28, 2022

Thanks for the update! How many refresh errors do you get?

@cruftex
Copy link
Member
cruftex commented Oct 28, 2022

I think the cause of your problem is, that by default cache2k is expiring (or refreshing) timer based and execution of timer events will always lag behind. This behavior is saving resources. With typical cache expiry times around 5 minutes, it does not matter if an update happens one second later.

For the purpose of testing you can add sharpExpiry(true) to the configuration. Then the test code should pass. See the documentation on expiryAfterWrite and sharpExpiry.

@javalover123
Copy link
Author
javalover123 commented Oct 28, 2022

Not lag minutes, but hours. For example 15:00(today) execute TIME_CACHE.get("") get yesterday.

PS: I update the test code.

@javalover123
Copy link
Author

I execute jstack per 0.5 second, there is a same thread "cache2k-loader-_Cache2kRefreshTest.CLINIT-22-dk-1" #30, and not saw the thread when cache not update.

image

@cruftex
Copy link
Member
cruftex commented Oct 28, 2022

What is the git revision of cache2k you are using? What Java runtime version you are using?

@javalover123
Copy link
Author
javalover123 commented Oct 28, 2022

git revision is 437c58c1ceb0cacaa9d2d282ad702e6aeed3e19d

Run on windows, jdk is below.
java version "11.0.12" 2021-07-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.12+8-LTS-237)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.12+8-LTS-237, mixed mode)

@cruftex
Copy link
Member
cruftex commented Oct 28, 2022

Ok, I will investigate.

In the code I am in the middle of some restructuring, however, I was held up by other projects since May.
The current unreleased code can have issues, since the restructuring is not totally finished yet. I still have around 25 code location where I need to double check things and do corrections.

If this is blocking your project maybe consider using the latest released final version. Before the next release I will do some more changes around the RefreshAheadPolicy, which will potentially break your code. Only after the final release I keep the interface stable.

Anyhow, it is great that you test with the latest code base and give feedback. That catches potential problems early on.

@cruftex
Copy link
Member
cruftex commented Oct 28, 2022

I execute jstack per 0.5 second, there is a same thread "cache2k-loader-_Cache2kRefreshTest.CLINIT-22-dk-1" #30, and not saw the thread when cache not update.

Do I assume correctly that if you start the application its sometimes not working at all? If so, approximately, out of how many starts it does fail?

My strategy is to add a configuration similar to yours to our test and run these tests many times to make sure there is no spurious problem within the initialization procedures.

@javalover123
Copy link
Author

Run 4 months, just happened once only.

@ieugen
Copy link
ieugen commented Nov 26, 2022

Are you using ECC memory on the servers?
Random bit flips might also be an issue if not using ECC.

@javalover123
Copy link
Author
javalover123 commented Nov 26, 2022

Are you using ECC memory on the servers? Random bit flips might also be an issue if not using ECC.

Maybe is non-ECC memory.

Get-WmiObject Win32_PhysicalMemory |
    Select-Object -Property PSComputerName, DeviceLocator, Manufacturer, PartNumber, @{label = "Size/GB" ; Expression = {$_.capacity / 1GB}}, Speed, datawidth, totalwidth, @{label = "ECC" ; Expression = {
        if ( $_.totalwidth -gt $_.datawidth ) {
            "$($_.DeviceLocator) is ECC memory type"
        }
        else {
            "$($_.DeviceLocator) is non-ECC Memory Type"
        }
    }
} | Out-GridView

image

@ieugen
Copy link
ieugen commented Dec 1, 2022

This might be the issue since you saw it only once in 4 month of operation.
Read a bit more here https://en.wikipedia.org/wiki/ECC_memory and other sources.

@ieugen
Copy link
ieugen commented Dec 1, 2022

If this is the case and there is no other information we might close the issue.

@javalover123
Copy link
Author

I execute jstack per 0.5 second, there is a same thread "cache2k-loader-_Cache2kRefreshTest.CLINIT-22-dk-1" #30, and not saw the thread when cache not update.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0