LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

Database outages cause problems

Open pongor8 opened this issue 8 years ago • 5 comments

Hello, teacher @mbdavid Before I by 1.0.0.4, upgrade to 4.0, use of themselves and their colleagues test everything is normal, but now gray after the release, 400 users, there have been two users appear problem, leading to the database file is damaged, read/write error and infinite loop again after damage, especially in the infinite loop problem is more serious. Causes of the problem: user said suddenly loses power in use.LiteDB done transactions and roll back the security mechanism, please?

In addition, the infinite loop in the 1.0.0.4 before, my approach is as follows: ///

/// Add a page in free list in desc free size order /// private void AddToFreeList(BasePage page, BasePage startPage, ref uint fieldPageID) { var free = page.FreeBytes; var nextPageID = fieldPageID; BasePage next = null;

        uint whileCount = 0;//出现死循环时使用。20171101xipeng修改
        // let's page in desc order
        while (nextPageID != uint.MaxValue)
        {
            whileCount++;
            next = this.GetPage<BasePage>(nextPageID);

            if (free >= next.FreeBytes)
            {
                // assume my page in place of next page
                page.PrevPageID = next.PrevPageID;
                page.NextPageID = next.PageID;

                // link next page to my page
                next.PrevPageID = page.PageID;
                next.IsDirty = true;

                // my page is the new first page on list
                if (page.PrevPageID == 0)
                {
                    fieldPageID = page.PageID;
                    startPage.IsDirty = true;
                }
                else
                {
                    // if not the first, ajust links from previous page
                    var prev = this.GetPage<BasePage>(page.PrevPageID);
                    prev.NextPageID = page.PageID;
                    prev.IsDirty = true;
                }

                page.IsDirty = true;

                return; // job done - exit
            }

            if (whileCount >= 1000*10000)
                throw new Exception("数据被损坏,进入死循环");
            nextPageID = next.NextPageID;
        }

The wrong reference: ToList Value cannot be null.Parameter name: source get_Item The given key was not present in the dictionary. FirstOrDefault Value cannot be null.Parameter name: source ToList Value cannot be null.Parameter name: source

pongor8 avatar Dec 22 '17 10:12 pongor8

Hi @pongor8, if application stops suddenly during write process, LiteDB uses journal file to recovery datafile to last valid state. Every time litedb try open datafile, initialize process checks if exists journal file. Jounal file contains original (non modified) pages.

Did you remove this in your code?

    private void CleanupJournal(string databasePath)
    {
        var file = databasePath.Replace(".db", "-journal.db");
        if (File.Exists(file))
            File.Delete(file);
    }

Because, if journal was deleted, you can't recovery your data!! If is this case, tell me....

mbdavid avatar Dec 22 '17 22:12 mbdavid

Hello, teacher @mbdavid Specially in v4.0 I delete (comments) for this code, no longer use CleanupJournal before, the purpose is to through the normal logic to running a database file, don't want to use remedy to deal with. Today I tested on my laptop, Windows 7, removing the battery and then insert the data, unplug the power, the result is not generated log files, and everything is ok. Strange, why there are so many database corruption phenomenon to appear on the user's computer. The following are examples of singleton I use database objects: public partial class NoSql { private static object obj_lock = new object(); private static Mongo.DB db = null; static NoSql() { if (db == null) db = new Mongo.DB(); }

public static bool Insert<T>(T obj) where T : class,new()
    {
        lock (obj_lock)
        {
            db.Insert<T>(obj);
            return true;
        }
    }

...

}

public class DB : IDisposable { LiteDatabase db = null; public DB() { if (!System.IO.Directory.Exists(SysDataSetting.CurrentUserStorePath)) System.IO.Directory.CreateDirectory(SysDataSetting.CurrentUserStorePath);

    string databasePath = SysDataSetting.CurrentUserStorePath + SettingModel.Setting.OperIcCode + ".db";
    //CleanupJournal(databasePath);
    if (db == null)
	db = new LiteDatabase(databasePath);
}

public void Insert<T>(T obj) where T : class,new()
    {
        string typeName = ConvertInsertTypeName(typeof(T).ToString());
        try
        {
            //获取 customers 集合,如果没有会创建,相当于表
            db.GetCollection<T>(typeName).Insert(obj);
            this.EnsureIndex<T>(db.GetCollection<T>(typeName), typeName);
        }
        catch (Exception ex)
        {
            ex.HandleException();
        }
    }

...

/// <summary>
    /// 为表添加索引。LiteDB 4.0版本需要的
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="col"></param>
    /// <param name="typeName"></param>
    private void EnsureIndex<T>(LiteCollection<T> col, string typeName)
    {
        switch (typeName)
        {
	case "IO_SaleReturn":
	    {
		col.EnsureIndex("FileName");
		col.EnsureIndex("CreationTime");
		col.EnsureIndex("CheckDateTime");
		break;
	    }
	    ...
    }
}

}

pongor8 avatar Dec 25 '17 04:12 pongor8

Can't confirm: had many failures over time - power cutoffs, CIFS availability problems(soft faults and/or sudden network disasters), RAID 1 drive faults, powerdowns due to cpu overheating with litedb file spanning ~56k entries. Not a single corruption.

Speaking of user: what if some weird AV software corrupts the file... or the journal, or a malware does all of this. Just a guess. It would be a nice thing to have: some ways to test the db file and get the idea why(to some degree) it is broken.

Itaros avatar Dec 25 '17 06:12 Itaros

thanks,@Itaros

pongor8 avatar Jan 02 '18 01:01 pongor8

Hi! With the objective of organizing our issues, we are closing old unsolved issues. Please check the latest version of LiteDB and open a new issue if your problem/question/suggestion still applies. Thanks!

lbnascimento avatar Feb 28 '20 13:02 lbnascimento