2 Samuel 17 and 18 describe a battle Absalom wages against David. God arranges for him to get bad counsel from his men. David is warned about the battle. He is counseled to remain at camp while his men go to war. He agrees and tells them to deal gently with Absalom. Absalom goes into battle with the army of Israel. David’s men defeat the army.
Absalom, riding a mule, gets caught in a tree and the mule walks away. Joab, one of David’s captains kills Absalom, against David’s wishes. The king hears of Absalom’s death and weeps.
While the defeat of Absalom is in God’s plan David still feels deep loss at the death of his son. Even though we may know that someone is dying and are somewhat prepared to hear the news it does not diminish the pain of loss.
Psalms 52-54 all deal with battles against those who would do the righteous harm, those who no longer seek God. David recognizes that God is on his side. He will deliver him from those who are out to do evil.